Before you start this, there are 2 ways to approach this and one is more expensive than the other from a compute transaction perspective.
Approach 1 - 3 comparisons - Not ideal but probably the first you will think of
You can easily compare 1v2, 1v3 and then 2v3 and then decide on the result somehow.
Attention to detail
However the question says that if there is a high number, just show it - notice how it doesn’t ask you to output whether the highest number was num1, num2 or num3?
This is important as interviewers like Amazon / Google will grill you on why this is the approach you took. You should always be thinking about optimization, compute cost and possibility of parallelization.
Approach 2 - 2 comparisons only
I believe there is a 2nd approach which is less compute cost as it iterates through your 3 numbers, it only remembers the highest number seen so far, and if it sees a higher one, it will remember the previous top number in a different variable and replace the highest number seen with the new winner.
You now only need to run 2 comparisons instead of 3.
To other posters posting solutions
The user at this stage of the course has not been introduced to arrays, max function, for loops, etc, so please try add basic answers to assist them.
I have started learning python today and never done it before. I could improve it further to add data validation and exception handling, but this answers the question. But during QA / unit testing, you should be entering symbols and letters into your function and make sure it handles it all correctly.
Here is my take on the code which seems to pass all the basic tests…
# Write your max_num function here:
def max_num(num1, num2, num3):
#index 0 check (we have only seen 1 number so far so this is the top number we have seen so far)
top_number = num1
medium_number = 0
#index 1 compare (compare num2 with highest number seen so far, if num2 is MTE, relegate the old top number we saw to medium_number, replace top_number with num2
if num2 >=top_number:
medium_number = top_number
top_number = num2
#index 2 compare (compare num3 with highest number seen so far, if num3 is MTE, relegate the old top number we saw to medium_number, replace top_number with num3
if num3 >=top_number:
medium_number = top_number
top_number = num3
#time to decide on the result, there are only be 2 results, a tie or a winner.
message = "It's a tie!"
message = top_number
# return the final message
# 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!"