If Statement Containing Multiple Strings

I apologize from the outset for the eyesore this probably is. I’m not even 100% sure how to properly phrase what I’m trying to do. I’m very new to Python and coding in general.

I am essentially trying to say:

    if "str" in "raw_input string":
        print "b"
    elif "raw_input string" contains "x" or "y" or "z":
        print "a"
    else:
        print "c"

I can get it to work if I type the following:

    if "str" in "raw_input string":
        print "b"
    elif "x" in "raw_input string":
        print "a"
    elif "y" in "raw_input string":
        print "a"
    elif "z" in "raw_input string":
        print "a"
    else:
        print "c"

I figure there has to be a more efficient way to do this though. Again, I’m sorry if this is a stupid question. I’m very new to this.

Here is what I may do in that case.

string = "raw_input string"

if "str" in string:
    print "b"
elif "x" in string or "y" in string or "z" in string:
    print "a"
else:
    print "c"

I imagine there is probably a better answer but this should do the trick.

1 Like

That worked perfectly! It’s definitely much shorter than what I was doing. Thank you! :grin:

My pleasure! You will be amazed by how quickly it will click if you keep at it!

I definitely hope so!

1 Like

Yep, you have to be very clear with Python and any programming language. If you think about, even the English sentence “I if me and my friend have clothes, or a rain coat, or an umbrella, then it’s raining” is wrong. You aren’t being clear in that sentence about who has the rain coat or umbrella. We kind of assume “my friend” does, but Python assumes nothing. You must write: “If my friend has clothes, or I have a rain coat, or my friend has an umbrella and I have an umbrella, then it’s raining.”

1 Like

Thanks, @zedshaw! It makes a lot of sense when you put it that way.