why does this return true, any explanations?
Question:
if 9266 > 9204 and 9271 and 9052 and 299:
print("true")
The biggest number is 9271 still it prints true, I have tried wrapping each value in round brackets too, did not help
Answers:
Imagine it like this: if (9266 > 9204) and bool(9271) and bool(9052) and bool(299): print("true")
and
is python’s &&
, the conditions are separated by it. In this case, this means that each number after 9266 > 9204
is interpreted as boolean, which will be true for anything that is not 0. This basically means, that the if
statement is equivalent to if 9266 > 9204 and True and True and True:
Any number different from zero will return ‘True’ in a condition.
So your if condition is evaluating 9266 > 9204 and True and True and True
which will always be True
.
I think what you want to do is :
if( (9266 > 9204) and (9266 > 9271) and (9266 > 9052) and (9266 > 299) :
print("true")
This way you can compare your first number 9266 to all the following number to see if it’s bigger. Another way to do it could be to determine the biggest number from a list using the max()
function in python and only compare if (9266 > biggest_number)
if 9266 > 9204 and 9271 and 9052 and 299:
print("true")
The biggest number is 9271 still it prints true, I have tried wrapping each value in round brackets too, did not help
Imagine it like this: if (9266 > 9204) and bool(9271) and bool(9052) and bool(299): print("true")
and
is python’s &&
, the conditions are separated by it. In this case, this means that each number after 9266 > 9204
is interpreted as boolean, which will be true for anything that is not 0. This basically means, that the if
statement is equivalent to if 9266 > 9204 and True and True and True:
Any number different from zero will return ‘True’ in a condition.
So your if condition is evaluating 9266 > 9204 and True and True and True
which will always be True
.
I think what you want to do is :
if( (9266 > 9204) and (9266 > 9271) and (9266 > 9052) and (9266 > 299) :
print("true")
This way you can compare your first number 9266 to all the following number to see if it’s bigger. Another way to do it could be to determine the biggest number from a list using the max()
function in python and only compare if (9266 > biggest_number)