Why doesnt this code return the correct answer?

I was doing the advanced control flow exercise_5 and i was wondering why the following wouldnt return the correct answers. I already know the correct solution but I’m just trying to further understand and if there’s a another cleaner way of doing it.

# Write your max_num function here:
def max_num(num1, num2, num3):
  if num1 > num2 and num3:
    return num1
  elif num2 > num1 and num3:
    return num2
  elif num3 > num1 and 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!"

The logic statements don’t work the way you’re using them. Try doing something like this outside your for loop-

if num1 > num2 and num3:

It’s effectively (num 1 > num2) and bool(num3). Edit: updated for accuracy courtesy of @ionatan

As for cleaner you can chain statements such that the following works but you’d need to make sure it was clear that was happening.

a, b, c = 1, 2, 3
a < b < c
Out: True

There’s a decent response on this at-

1 Like

unless 0
(kinda beside the point though)

1 Like

That’s more accurate aye. Or None.

As for cleaner I suggest generalizing it to accept zero or more inputs rather than 3