I’m trying to get an if statement to trigger from more than one condition without rewriting the statement multiple times with different triggers. e.g.:
if user_input == "look":
print description
if user_input == "look around":
print description
How would you condense those into one statement?
I’ve tried using ‘or’ and it caused any raw_input at all to trigger the statement regardless of whether the input matched either of the conditions.
if user_input == "look" or "look around":
print description
What you’re trying to do is
Another option if you have a lot of possibilities:
Since you’re using 2.7, you could also write it like this (which works in 2.7 or 3+, but not in 2.6 or below):
which makes a
setof your elements, which is very slightly faster to search over (though that only matters if the number of elements you’re checking is much larger than 2).The reason your first attempt always went through is this. Most things in Python evaluate to
True(other thanFalse,None, or empty strings, lists, dicts, …).ortakes two things and evaluates them as booleans. Souser_input == "look" or "look around"is treated like(user_input == "look") or "look_around"; if the first one is false, it’s like you wroteif "look_around":, which will always go through.