Python Script returns unintended "None" after execution of a function [duplicate]
Background: total beginner to Python; searched about this question but the answer I found was more about "what" than "why";
What I intended to do: Creating a function that takes test score input from the user and output letter grades according to a grade scale/curve; Here is the code:
score = input("Please enter test score: ")
score = int(score)
def letter_grade(score):
if 90 <= score <= 100:
print ("A")
elif 80 <= score <= 89:
print ("B")
elif 70 <= score <= 79:
print("C")
elif 60 <= score <= 69:
print("D")
elif score < 60:
print("F")
print (letter_grade(score))
This, when executed, returns:
Please enter test score: 45
F
None
The None
is not intended. And I found that if I use letter_grade(score)
instead of print (letter_grade(score))
, the None
no longer appears.
The closest answer I was able to find said something like "Functions in python return None unless explicitly instructed to do otherwise". But I did call a function at the last line, so I'm a bit confused here.
So I guess my question would be: what caused the disappearance of None
? I am sure this is pretty basic stuff, but I wasn't able to find any answer that explains the "behind-the-stage" mechanism. So I'm grateful if someone could throw some light on this. Thank you!
In python the default return value of a function is None
.
>>> def func():pass
>>> print func() #print or print() prints the return Value
None
>>> func() #remove print and the returned value is not printed.
>>>
So, just use:
letter_grade(score) #remove the print
Another alternative is to replace all prints with return
:
def letter_grade(score):
if 90 <= score <= 100:
return "A"
elif 80 <= score <= 89:
return "B"
elif 70 <= score <= 79:
return "C"
elif 60 <= score <= 69:
return "D"
elif score < 60:
return "F"
else:
#This is returned if all other conditions aren't satisfied
return "Invalid Marks"
Now use print()
:
>>> print(letter_grade(91))
A
>>> print(letter_grade(45))
F
>>> print(letter_grade(75))
C
>>> print letter_grade(1000)
Invalid Marks
Functions without a return statement known as void functions return None from the function. To return a value other than None, you need to use a return statement in the functions. Values like None, True and False are not strings: they are special values and keywords in python which are being reserved. If we get to the end of any function and we have not explicitly executed any return statement, Python will automatically return the value None. For better understanding see the example below. Here stark isn't returning anything so the output will be None
def stark(): pass
a = stark()
print a
the output of above code is:
None
Here is my understanding. A function will return "none" to the console if a value is not given to return. Since print is not a value, if you use print() as the only thing you want your function to do, 'none' will be returned to the console, after your printed statement. So, if the function wants a value, and you want a string to be that value returned...
Give your returned statement the value of a string like this... Here is a very basic example:
def welcome():
return str('Welcome to the maze!')
Then print the function where you intend for it to be:
print(welcome()):
Result is:
Welcome to the maze!