Greater than and equal to

Hey everyone for the life of me I can not figure out this one.

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!" .

I know that I need an if/else statement by doing this

def max_num(num1, num2, num3):
    if num1 < num2 and num1 < num3:
# and so on but I dont get how to also see if its a tie or does that just go in the else statement and how do # I write that
# Write your max_num function here:

# 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!"

One way to think about it is what happens with a subtraction operation? Are there any other properties that are useful?

It helps to write the flow on paper…

For subtraction:

  • how many outcomes are possible?
  • which are the outcomes that fit the condition of greater than?
  • (note that there is a greater than operand, >= )

We can actually potentially save work and have more clarity in the program by noticing additional properties of differences between 3 numbers.

Order is associative (by order I mean, when things are greater, equal, or less than).
So you know that

  • if (a >= b and b >= c)
  • then a >= c.

Note that the contrapositive would offer

  • if a <= c
  • then (a <= b or b <= c).

This seems super simple perhaps but it turns out it there are other operations that share the same principles of ordering, and if you can catch those patterns, your code will always improve for it.


Ok I get it. So I should have.

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
        return "It's a tie"
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.