Why does my function print none?

Trying to figure out why my code doesn’t work. It works great for the first 3 test calls but returns None for the tie. Here is the code:

def max_num(num1, num2, num3):
  if num1 > num2:
    if num1 > num3:
      return num1
  if num2 > num1:
    if num2 > num3:
      return num2
  if num3 > num1:
    if num3 > num2:
      return num3
  else:
    return "It's a tie!"

It doesn’t look like it in the post, but the first test for each numx variable is indented 2 spaces with the following line indented another 2 spaces. The else is indented 2 spaces at the same level as the initial if.

I also changed the 2nd and 3rd ‘main’ ifs to elif. Doing that added another None return for the test call where num3 was the largest (1st test call)
I’m mostly wondering why my first attempt did not work for the tie.

None is a value displayed by python when there is no explicit return. This is useful to understand

using if or elif has quite large implications on how your program behaves, you need to think about the logic, break it down in steps

in the code you posted, this test case fails:

print(max_num(2, 3, 3))

why? Because else only runs when if num3 > num1: is false, but if is true, so else won’t run.

1 Like

Thank you! I appreciate the help.

So i wrote this code:

def max_num(num1, num2, num3):
  if num1 > num2 and num2 > num3:
    return num1
  elif num1 < num2 and num2 > num3:
    return num2
  elif num1 < num2 and num2 < num3:
    return num3
  else:
    return "It's a tie!"

but its giving me an error because it wont pick up a negative sign in one of the prints:

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 but it prints This is a tie**
print(max_num(2, 3, 3))
# should print "It's a tie!"

the problem is actually here:

if num1 > num2 and num2 > num3:

lets say we have: max_num(8, 4, 6)

then 8 should be returned, but this isn’t happening, because number two isn’t larger then number three. So are you sure you got your conditions right? Write down for yourself in plain text what should happen here

Try this code
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num3 and num2 > num1:
return num2
elif num3 > num2 and num3> num1:
return num3
else:
return “It’s a tie!”

I wrote this

def max_num(num1,num2,num3):
if num1> num2 and num3:
return num1
elif num2> num1 and num3:
return num2
elif num3> num2 and num1:
return num3
else:
return “It’s a tie!”

I’m wondering why it prints “It’s a tie!”? If numx isn’t as large as numy and numz should it only return numx? and all the others fail?

We need to perform the comparison on both sides of and.

if a > b and a > c
2 Likes
def max_num(num1,num2,num3):

#create a list within the three given arguments:
  myList = [num1,num2,num3]

#Check if the length of myList is different than the set of it (wich means it contains double numbers) and returns "Its a tie!" if it's different:
  if len(myList) != len(set(myList)):
    return "It's a tie!"

#Otherwise return the max() value of the given arguments:
  else:
    return max(num1,num2,num3)

your if statement for checking for ties doesn’t necessarily work. If for example, you called

max_num(1,1,2)

your code would return “It’s a tie!” even though the max number isn’t in a tie with any other number.

HELP!

I wrote the same code as you but I putted conditions on the else but I got a Syntax error:

def max_num(num1 , num2 , num3):
if num1 > num2 and num1> num3:
return num1
elif num2 > num1 and num2 >3:
return num2
elif num3 > num1 and num3 > num2:
return num3
else num1 == num2 or num1 == num3 or num2 == num3:
return “It’s a tie!”

if I change the else with another elif it works fine and no Syntax Error shows up.

WHY???

Because else is not conditional. It is the fallback or default branch, if such is to be available. Not all if statements will need it.

if x > 5:
    return 1
elif x < 5:
    return -1
else:
    return 0
1 Like

Hello, I’m curious as to why my code returns “None” between the correct answers. And this happened to other Code Challenges exercise as well.

EDIT: I changed the ‘print’ into ‘return’ and I got correct answers (without the “None”). Does this mean if I use ‘print’ a “None” will follow? Why is that?

10
None
5
None
-5
It’s a tie!
None
It’s a tie!
None

def max_num(num1, num2, num3):
  if num1 > num2 and num1 > num3:
    print(num1)
  if num2 > num1 and num2 > num3:
    print(num2)
  if num3 > num1 and num3 > num2:
    print(num3)
  if num1 == num2 or num1 == num3 or num2 == num3:
    print("It's a tie!")

Hello, @micro3283005873.

Every function in Python returns something. If we don’t explicitly return a_value, then the function will implicitly return None. When we invoke a function as the argument to a print() function, print() is going to print whatever is returned. Since your function includes print() functions, the values you supply to print() inside the function are printed. Then in the absence of an explicit return, None is implicitly returned, and printed by the print() function that included the call to your function as its argument: print(max_num(arg1, arg2, arg3))
Consider:

def print_something(val):
    print(val) #This will print "Hello, World!"
    
def return_something(val):
    return val
    
print(print_something("Hello, World!")) #This will print the returned value: None

print(return_something("Hello, World!")) #This will print the returned value: Hello, World!

Output:

Hello, World!
None
Hello, World!

We can simply call a function that doesn’t have an explicit return, and only prints values:

def print_something(val):
    print(val) #This will print "Hello, World!"
    
print_something("Hello, World!") #call the function

Output:

Hello, World!

In this case, None is still implicitly returned, but we haven’t done anything with it.

2 Likes

Hello @midlindner, thank you so much for the clear explanation!

1 Like