FAQ: Code Challenge: Control Flow - Max Number

#23

There is plenty of documentation to explain how max() works. The purpose of this exercise is not to study the built in function, but to create a one of our own that performs the same task, using an algorithm.

1 Like
#24

This is my code. In case of call - print(max_num(5, 3, 3)) - it prints only 5, although it seems to be “It’s a tie!” also, because of if (not elif) operators. Am I wrong?

def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
if num2 > num1 and num2 > num3:
return num2
if num3 > num1 and num3 > num2:
return num3
if num1 == num2 or num1 == num3 or num2 == num3:
return “It’s a tie!”
print(max_num(5, 3, 3))

#25

Return means handing back data to the function call, thus when the code reaches the line return num1 the function ends, so it never does the tie comparison