Mix n Match


Mix n Match

Make me false!

bool_three = 2**2 and 3**2 >= 100 or 5**2

Can somebody tell me what I am doing wrong with above?


Hi @ianlongchamp,

See the Python Operator precedence table.

That table lists the order in which the Python interpreter performs operations in evaluating expressions. Operations listed at the top are performed last. As the table indicates, and and or operations are performed after ** and >= operations.

Your code assigns 25 to bool_three, when it should assign False to the variable. That is because the or operation is performed last, resulting in the value of the subexpression, 5**2, becoming the resulting value of the entire expression.

To revise your code, you can use parentheses to control the order of the operations that you already have, or you can use different operations. The second choice might be more advisable in this case.

EDIT (April 20, 2017): Be aware that some operator precedence tables list the operations in the opposite order, therefore it is important to check the explanation accompanying the table in each case.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.