At the first return the function exits. Control flow never reaches the conditional. Perhaps you meant the following, checking for a tie, otherwise returning the maximum number.
def max_num(num1, num2, num3): if num1 == num2 or num1 == num3 or num2 == num3: return "It's a tie" else: return max(num1, num2, num3)
As a sidenote, another way to check for a tie would be to check the length of a set created from the three numbers. A set will not contain duplicates, so if it's less than 3, there must be a tie.
def max_num(num1, num2, num3): if len(set([num1, num2, num3])) != 3: return "It's a tie" else: return max(num1, num2, num3)
Rather than returning a string from your function in the event of a tie, you may wish to raise an exception so the calling code can handle that situation as it sees fit.
class TieException(Exception): pass def max_num(num1, num2, num3): if len(set([num1, num2, num3])) != 3: raise TieException return max(num1, num2, num3) try: print(max_num(1, 2, 2)) except TieException: print("It's a tie")
Note that else is not necessary here because raising the exception also forces control flow to leave the function. The return will not be evaluated if the fist condition is true.
You might also use count to check for a tie situation.
def max_nums(num1, num2, num3): nums = [num1, num2, num3] if any(nums.count(n) > 1 for n in nums): raise TieException return max(nums)
This has the benefit of scaling so that any number of numbers can be passed in. Though you may wish to raise a TypeError exception if no numbers are provided.
def max_nums(*nums): if len(nums) == 0: raise TypeError("max_nums requires at least one argument") if any(nums.count(n) > 1 for n in nums): raise TieException return max(nums)
Of course, we also don't need to know the count. This would be very inefficient for a large list. Instead we just need to check if any number is in the rest of the list after it. This will stop as soon as it finds a duplicate, indicating a "tie."
def max_nums(*nums): if len(nums) == 0: raise TypeError("max_nums requires at least one argument") if any(x in nums[i+1:] for i, x in enumerate(nums)): raise TieException return max(nums)
ifstatement that might preclude it from running?