Why does my recursive function return None?
It is returning None
because when you recursively call it:
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
..you don't return the value.
So while the recursion does happen, the return value gets discarded, and then you fall off the end of the function. Falling off the end of the function means that python implicitly returns None
, just like this:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just calling get_input()
in your if
statement, you need to return
it:
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
return get_input()
return, return None, and no return at all?
On the actual behavior, there is no difference. They all return None
and that's it. However, there is a time and place for all of these.
The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.
Using return None
This tells that the function is indeed meant to return a value for later use, and in this case it returns None
. This value None
can then be used elsewhere. return None
is never used if there are no other possible return values from the function.
In the following example, we return person
's mother
if the person
given is a human. If it's not a human, we return None
since the person
doesn't have a mother
(let's suppose it's not an animal or something).
def get_mother(person):
if is_human(person):
return person.mother
else:
return None
Using return
This is used for the same reason as break
in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.
We've got 15 prisoners
and we know one of them has a knife. We loop through each prisoner
one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners
. If we don't find the prisoner
with a knife, we raise an alert. This could be done in many different ways and using return
is probably not even the best way, but it's just an example to show how to use return
for exiting a function.
def find_prisoner_with_knife(prisoners):
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition()
return # no need to check rest of the prisoners nor raise an alert
raise_alert()
Note: You should never do var = find_prisoner_with_knife()
, since the return value is not meant to be caught.
Using no return
at all
This will also return None
, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return
in void
functions in languages such as C++ or Java.
In the following example, we set person's mother's name and then the function exits after completing successfully.
def set_mother(person, mother):
if is_human(person):
person.mother = mother
Note: You should never do var = set_mother(my_person, my_mother)
, since the return value is not meant to be caught.
Why is this printing 'None' in the output?
Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None
.
Use return
statement at end of function to return value.
e.g.:
Return None
.
>>> def test1():
... print "In function."
...
>>> a = test1()
In function.
>>> print a
None
>>>
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>>
Use return statement
>>> def test():
... return "ACV"
...
>>> print test()
ACV
>>>
>>> a = test()
>>> print a
ACV
>>>
Python Script returns unintended None after execution of a function
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
Why is None printed after my function's output?
It's the return value of the function, which you print out. If there is no return statement (or just a return
without an argument), an implicit return None
is added to the end of a function.
You probably want to return the values in the function instead of printing them:
def jiskya(x, y):
if x > y:
return y
else:
return x
print(jiskya(2, 3))
Why does this function return None None as well?
Every function always returns a value. If you don't explicitly return a value, and the function just gets all the way to the end, then it automatically returns None. Your function happy
doesn't have any return
statement, so at the end of the function, it automatically returns None.
why python is returning none after calculation?
multiplicationTable(number)
has no return value.
By default, when no return
value is given, a python function will return None
to signify that it was successfully executed. You have the same behavior in other languages.
To illustrate this, I made your function return something at the end :
def multiplicationTable(number):
for x in range(1, 11):
result = number * x
print(f'{number} X {x} = {result}')
return 'I have finished'
result = multiplicationTable(5)
print(result)
Output :
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
I have finished
Related Topics
Process Escape Sequences in a String in Python
Spawning Multiple Instances of the Same Object Concurrently in Python
String Comparison in Python: Is Vs. ==
Why Does Substring Slicing With Index Out of Range Work
How to Find the Location of My Python Site-Packages Directory
Matplotlib/Seaborn: First and Last Row Cut in Half of Heatmap Plot
How to Write the Fibonacci Sequence
How to Select a Variable by (String) Name
Use a List of Values to Select Rows from a Pandas Dataframe
Is Explicitly Closing Files Important
Why Is Python Running My Module When I Import It, and How to Stop It