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