Reputation: 53
I'm just starting out learning Python and wanted some practice with if statements since I'm playing around with text-adventure games. I was trying to get a thing like this to work. For instance, if one would type "look at the floor" (or just "look floor"), where word1 would be 'look' and 'floor'.
Perhaps there is an easy answer to this, and I've tried a few different ways, but can't seem to make it work. Thanks for any help!
def test():
answer = raw_input().lower()
if ('word1' and 'word2') in answer:
print "Both words were in the answer."
else:
print "Both words were NOT in the answer"
test()
Upvotes: 1
Views: 919
Reputation: 3829
The other answers are good, but I will try to explain what is wrong with your logic for you:
if ('word1' and 'word2') in answer:
According to the Python order of evaluation rules the components within the brackets of your if
statement are evaluated first.
Therefore your expression is effectively evaluated by the interpretter as:
temp = ('word1' and 'word2')
if temp in answer:
print "Both words were in the answer."
the value of temp
above will be the result of logically and
ing two different strings together which doesn't really make a lot of sense. In this case, the first operand evaluates to True
and the second operand is returned, if the first operand evaluated to False
(such as an empty string) Python would return the first operand. Thus in your specific case, Python will just return the second string, so:
('word1' and 'word2') == 'word2'
Therefore the interpretter reads your code as:
if ('word2') in answer:
print "Both words were in answer."
By writing:
if ('word1' in answer) and ('word2' in answer):
print "Both words were in answer."
You make the comparisons you want to evaluate explicit so the interpretter can understand and doesn't give you weird results.
Upvotes: 0
Reputation: 3921
Here is a more thorough explanation:
You have to keep in mind that while some idiomatic constructs make sense in English (or any other language), they might not make sense in a programming language, even if it is valid syntax.
For instance, let us consider the test expression your sample code:
('word1' and 'word2') in answer
When you apply the rules for the order of evaluation, the sub-expression in parentheses ( ('word1' and 'word2')
) is evaluated first. Because of the and
operator, the result of this sub-expression is the right operand since the left operand evaluates to True
. Re-inserting this value in your initial expression gives us this: 'word2' in answer
. Therefore the test will always validate as long as the second word can be found in the answer.
Edit: corrected boolean evaluation.
Upvotes: 2
Reputation:
You need to have an in
membership test for each word:
if ('word1' in answer) and ('word2' in answer):
Of course, this quickly becomes tedious if you have many words. In that case, you can use all
and a generator expression:
if all(word in answer for word in ('word1', 'word2', ...)):
The above will test if each word in the tuple ('word1', 'word2', ...)
can be found in answer
.
Upvotes: 3
Reputation: 122336
You can use all
to make that happen:
def test():
answer = raw_input().lower()
if all(word in answer for word in ('word1', 'word2')):
print "Both words were in the answer."
else:
print "Both words were NOT in the answer"
test()
This will go through each word that you specified and check whether it's in answer
. The built-in function all()
will return True
when that's the case for all checks that were performed, and False
otherwise. In other words, it's only True
when all checks are True
.
Upvotes: 2