Python Code Challenges: Control Flow (Advanced) Number 5

https://www.codecademy.com/paths/data-science/tracks/dscp-python-fundamentals/modules/dscp-python-control-flow/articles/advanced-python-code-challenges-control-flow

Create a function called max_num() that has three parameters named num1 , num2 , and num3 .

The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return "It's a tie!" .

Write your max_num function here:

def max_num(num1, num2, num3):
if(num1 > num2 and num1 > num3):
return str(num1)
elif(num2 > num1 and num2 > num3):
return str(num2)
elif(num3 > num1 and num3 > num2):
return str(num3)
elif(num1 == num2 or num1 == num3 or num2 == num3):
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!”

print(max_num(5, 2, 0))

When I click ‘Check Answer’, it gives me "max_num(5, 2, 0) should have returned 5, and it returned 5. I’m not sure if it is flagging my response as wrong because my logic is wrong somewhere in my code, or if it is just incorrect. When I run my code the last line prints in the counsel as 5.

Thank you in advance.

What happens when you return numbers instead of strings?

1 Like

Oh my, the example above had me return the numbers as strings so I just did it out of habit and glanced over that. Thank you, I was so confused.

1 Like