Print function result but two line with a None is printed
Question:
Create a function named same_name()
that has two parameters named
your_name
and my_name
.
If our names are identical, return True
. Otherwise, return False
.
My code:
def same_name(your_name, my_name):
if your_name == my_name:
print(True)
else:
print(False)
print(same_name("Colby", "Colby"))
print(same_name("Tina", "Amber"))
The output is:
print(same_name("Colby", "Colby"))
# True
# None
print(same_name("Tina", "Amber"))
# False
# None
But the expected output is:
print(same_name("Colby", "Colby"))
# True
print(same_name("Tina", "Amber"))
# False
Where None
is coming from?
Answers:
return, not print
It’s a common mistake to print instead of return. If a function need to return a value, print
won’t do it, it’s just for the user, commonly used to debug. Since there is no return statement in your function, the function return None by default. In print(same_name("Colby", "Colby"))
, you print the returned value of the function, which is None
, so you print a second time (the first one is in the function) with a None
value.
if your_name == my_name:
return True
else:
return False
Usually, there is two types of exercises, those who ask you to create/code a function, and those who doesn’t.
When you don’t need a function:
Example with your exercise:
Ask two names to the user. If the first name is equal to the second
name, print True
, if not, print False
.
The expected code would be something like this:
name_1 = input('name?')
name_2 = input('name?')
if name_1 == name_2:
print(True)
else:
print(False)
or the shorter alternative:
name_1 = input('name?')
name_2 = input('name?')
print(name_1 == name_2)
When you do need a function:
Example with your exercise:
Create a function called same_name
that takes two names. If the first name are equal
to the second name, return True
, if not, return False
.
The expected code would be something like this:
def same_name(name_1, name_2):
if name_1 == name_2:
return True
else:
return False
or the shorter alternative:
def same_name(name_1, name_2):
return name_1 == name_2
If you run the program with the function, you won’t print anything, even if you call the function like same_name("Colby", "Colby")
. It’s because return
doesn’t print anything, it quit the function with a value. That’s why they surrounded the function call with print
, to print the returned value, see the result and make sure it match the expected behavior.
Notes: The recommended practice is to return the condition result
The recommended practice is to return the condition result instead of branching with if:
def same_name(your_name, my_name):
return your_name == my_name
Create a function named
same_name()
that has two parameters named
your_name
andmy_name
.If our names are identical, return
True
. Otherwise, returnFalse
.
My code:
def same_name(your_name, my_name):
if your_name == my_name:
print(True)
else:
print(False)
print(same_name("Colby", "Colby"))
print(same_name("Tina", "Amber"))
The output is:
print(same_name("Colby", "Colby"))
# True
# None
print(same_name("Tina", "Amber"))
# False
# None
But the expected output is:
print(same_name("Colby", "Colby"))
# True
print(same_name("Tina", "Amber"))
# False
Where None
is coming from?
return, not print
It’s a common mistake to print instead of return. If a function need to return a value, print
won’t do it, it’s just for the user, commonly used to debug. Since there is no return statement in your function, the function return None by default. In print(same_name("Colby", "Colby"))
, you print the returned value of the function, which is None
, so you print a second time (the first one is in the function) with a None
value.
if your_name == my_name:
return True
else:
return False
Usually, there is two types of exercises, those who ask you to create/code a function, and those who doesn’t.
When you don’t need a function:
Example with your exercise:
Ask two names to the user. If the first name is equal to the second
name, printTrue
, if not, printFalse
.
The expected code would be something like this:
name_1 = input('name?')
name_2 = input('name?')
if name_1 == name_2:
print(True)
else:
print(False)
or the shorter alternative:
name_1 = input('name?')
name_2 = input('name?')
print(name_1 == name_2)
When you do need a function:
Example with your exercise:
Create a function called
same_name
that takes two names. If the first name are equal
to the second name, returnTrue
, if not, returnFalse
.
The expected code would be something like this:
def same_name(name_1, name_2):
if name_1 == name_2:
return True
else:
return False
or the shorter alternative:
def same_name(name_1, name_2):
return name_1 == name_2
If you run the program with the function, you won’t print anything, even if you call the function like same_name("Colby", "Colby")
. It’s because return
doesn’t print anything, it quit the function with a value. That’s why they surrounded the function call with print
, to print the returned value, see the result and make sure it match the expected behavior.
Notes: The recommended practice is to return the condition result
The recommended practice is to return the condition result instead of branching with if:
def same_name(your_name, my_name):
return your_name == my_name