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

Asked By: BlackPanther4592

||

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:

Answered By: 0x150

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)

Answered By: Kupofty
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.