Does anyone know why this IF request in Python is returning true? Here is the code:
var1 = 1
if var1 == 100 or 50:
print "yes"
else:
print "no"
I can't work it out because var1 does not equal either 100 or 50.
Does anyone know why this IF request in Python is returning true? Here is the code:
var1 = 1
if var1 == 100 or 50:
print "yes"
else:
print "no"
I can't work it out because var1 does not equal either 100 or 50.
For the if statement, it reads if (var1 == 50) or 50
50 is True
, so "yes" is printed out.
If you want to see if var1
is 100 or 50, you can do this:
var1 = 1
if var1 == 100 or var1 == 50:
print "yes"
else:
print "no
This if var1 == 100 or 50:
is not working the way you think it is.
Using parenthesis it actually looks like this if(var1 == 100) or 50
.
50 is not zero (zero is inherently False
, integers that are not zero, positive or negative, are inherently True
) so it will always evaluate True
Use this instead:
if var1 in (100,50):
I think your semantics is incorrect:
if var1 == 100 or var1 == 50:
Seems more likely.