Advanced Python Code Challenges: Control Flow, 5/5

Continuing the discussion from Advanced Python Code Challenges: Control Flow, Task 5 "Max Number:

I hope this thread is still active, since I also have a question about the elegance of this code.

To test if number 1 is greater than the other two numbers, why can’t we write it like:

if num1 > (num2 and num3):
    return num1

instead of:

if num1 > num2 and num1 > num3:
    return num1

I know for sure my code is wrong (since I didn’t get the desired output), I however don’t see why. Can somebody enlighten me? Thank you in advance :slight_smile:

We can’t write it like that because logical operators have special ways of evaluating.

x and y is going to be evaluated as 2 separate expressions and will yield either True or False.

So when you write num1 > (num2 and num3) it’s either going to be num1 > True or num2 > False.

1 Like