I wrote this bit of unnecessarily complicated code on my way to learning about how to use the in
statement to make if
statements work better. I have two questions following the code snippet.
answer = ['Yes', 'yes', 'YES']
answer2 = ['No', 'no', 'NO']
ans = raw_input()
for i in range(0, 3):
if ans in answer[i]:
print "Yes!"
elif ans in answer2[i]:
print "No!"
else:
print "Don't know what that means"
First question: I think if n in listname:
returns as True or False automatically. Does anyone know if that's the case?
Second question: the above code returns 3 lines that vary depending on if ans
is actually in answer
or answer2
. I tried to eliminate that by replacing the relevant portions like so:
if ans in answer[i] == True:
This had the strange effect of making the code output only the else:
statement. So can anyone explain to me the difference between how python interprets if ans in answer[i]:
and if ans in answer[i] == True:
, please?
If not true means the ‘not operator ‘ is used in the if statements in Python. The ‘not’ is a Logical operator in Python that will return True if the expression is False. If var is True, then not will evaluate as false, otherwise, True.
A conditional statement always generates a boolean output that is either true or false. ( Note that true is represented as True in Python and false as False ). After generating the output, the statement decides whether to move inside the conditional block or leave it as it is.
If <expr> is false, then <statement> is skipped over and not executed. Note that the colon (:) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not. Here are several examples of this type of if statement:
If an if statement is simple enough, though, putting it all on one line may be reasonable. Something like this probably wouldn’t raise anyone’s hackles too much: Python supports one additional decision-making entity called a conditional expression.
To answer your questions in reverse order, the reason why explicitly comparing with True
did not work for you is that Python did not interpret the expression they way you expected. The Python parser has special handling of compare expressions so that you can chain them together and get a sensible result, like this:
>>> "a" == "a" == "a"
True
Notice that Python has to treat this whole thing as one operation, because if you split it into two operations either way you don't get the same result:
>>> ("a" == "a") == "a"
False
>>> "a" == ("a" == "a")
False
These behave differently because the part in the parentheses is evaluated first and returns True
, but True != "a"
so the whole expression returns false.
By rights the above shouldn't actually have any impact on your program at all. Unfortunately, Python handles in
via the same mechanism as ==
so when you chain these together they are interpreted as a sequence like the above, so Python actually evaluates it as follows:
>> "a" in ["a"] == True
False
>>> ("a" in ["a"]) and ("a" == True)
False
It's wacky and arguably counter-intuitive, but that's unfortunately just how it works. To get the behavior you wanted you need to use parentheses to force Python to evaluate the first part separately:
>>> ("a" in ["a"]) == True
True
With all of that said, the == True
is redundant because, as you suspected, the expression already returns a boolean and the if
statement can just evaluate it as-is.
To return now to your other problem, I believe what you're trying to do is take one line of input and produce one corresponding line of output depending on what the user entered. You can apply the in
operator to a string and a list to see if the string is in the list, which allows you to eliminate your for
loop altogether:
answer = ['Yes', 'yes', 'YES']
answer2 = ['No', 'no', 'NO']
ans = raw_input()
if ans in answer:
print "Yes!"
elif ans in answer2:
print "No!"
else:
print "Don't know what that means"
This first tests if the input matches any of the strings in answer
, then the same for answer2
. Of course, you could achieve a similar effect but also support other forms like YeS
by simply converting the input to lowercase and comparing it to the lowercase form:
if ans.lower() == "yes":
print "Yes!"
# (and so forth)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With