The reason why this doesn't work is because or
is checking the truthiness of the two statements, it doesn't work to compare multiple values, only to OR
multiple booleans. So what you really have is
if ("breath") or (("air" in ans) == True):
Specifically, this is not checking if "breath"
is in ans
, but instead checking if breath:
. More on that at the bottom of the answer.
You can check if both values are in ans
by writing out the in ans
part twice, like this:
if "breath" in ans or "air" in ans:
or if you think you will wind up checking a ton of values, you can do a list like the other answer mentioned:
wordlist = ["breath", "air", ...]
contains_word = False
for word in wordlist:
if word in ans:
contains_word = True
break
if contains_word:
#do stuff
Note: "breath
" is being evaluated for truthiness by the or
. In python, non-empty strings are "truthy", and empty strings are "falsy". If you do "breath"==True
then you would get False
, but if you do if "breath":
, then the if
statement would evaluate as True
. Whereas if "":
would evaluate as False
.