Python Code Challenge(Advanced) - 5. Max Number - how does it work?!

Hello there :grinning_face_with_smiling_eyes: i completed the code challenges in the chapter “Control Flow and Control Flow(Advanced”) and im super proud :speak_no_evil:

I got the last challenge right, however it was more trial and error and pure luck and i still don’t really understand how the code executes and flows. Could someone possibly explain this to me? :grimacing:

I know the if statement checks for True :white_check_mark: and False :x:

I don’t really understand how the values which are “10,5,-5” are returned in this example. I don’t get really get why it returns the right number i don’t understand the flow :sleepy: :flushed:

For reference here is the full code:

def max_num(num1, num2, num3): if num1 > num2 and num1 > num3: return num1 elif num2 > num1 and num2 > num3: return num2 elif num3 > num1 and num3 > num2: return num3 else: return("It's a tie!") # Uncomment these function calls to test your max_num function: print(max_num(-10, 0, 10)) # should print 10 print(max_num(-10, 5, -30)) # should print 5 print(max_num(-5, -10, -10)) # should print -5 print(max_num(2, 3, 3)) # should print "It's a tie!"

you have the three parameters num1, num2, num3
If num1 is the biggest number,
num1 > num2 is true
num1 > num3 is true
(We have to check num1 is bigger than the other two in order to say that num1 is the maximum)

num1 > num2 and num1 > num2 would only be true if both are true,
therefore this is used in the if-statement that returns num1 (because the function should return the maximum of the three numbers, and num1 is the maximum in that case)

if num1 is not the biggest number,
then num1 > num2 would be false or num1 > num2 would be false
num1 > num2 and num1 > num2 would be false (since false and true gives you false)

and then the computer checks the elif statement
just like the previous stuff, but this time checking whether num2 is the maximum

and so on …


When using the and operator in control flow…both sides of the if statement have to evaluate to True for the return statement to execute. When the if evaluates to False, the return is not executed and it moves to the elif statement , etc. One thing to remember about Boolean operators is this: any and expression that is False is always False, any or expression that is True is always True. You can try it in a terminal window:

True and False
True or False
True and True

evaluates to False
evaluates to True
evaluates to True


It might help to review boolean operators and conditionals in Python too.

1 Like

i still dont fully understand how it returns 10 in the first lines of code (and same for the later)
i made an example how i see the code

Bildschirmfoto 2021-07-21 um 18.55.47

in my head its like this: it returns num1, which is “-10” and this alone confuses me, i dont understand even with true and false how the computer knows it has to be 10. of course there is a remaining number but i still dont understand it.

When I get stuck like this I find that it’s helpful to write things out so I can see what’s happening with the function logic.

So, the arguments passed through the function are (-10, 0, 10):

if (-10 > 0) and (-10 > 10):
    return num1

Is -10 > 0? False AND is -10 >10? False
So, the first statement is False, and the return statement is not executed. The compiler goes to the elif statement…

elif (num2 > num1) and (num2 > num3):
     return num2

Is 0 > -10? YES, so, True AND 0> 10? NO, so, False. True AND False equates to…False.
and so on until the conditions are met and are True, which happens in the 2nd elif,

elif (num3 > num1) and (num3 > num2):
     return num3

is (10 > -10)? True AND is (10 > 0)? True…so, True and True evaluates to True, and num3 is returned, so, 10.

Try passing through a bunch of different numbers to see how the logic works.

To understand more on Boolean logic, maybe google it to read up of George Boole too:](
and here:

And, tbh, there are people on this forum that know way more than I do about how computers operate. :slight_smile:


thank you very much this helps me a lot! I didnt understand that the code continues that way. My head was stuck at the first lines, lol!