Hi: I am testing the recursive function like this:
def countdown(n):
if n == 1:
print 1
elif n > 1:
print countdown(n - 1), '\n', n
else:
pass
The function is to count down from $n$ to $1$. but why I get 'None' in the output?
In [2]: countdown(4)
1
None
2
None
3
None
4
Thank you.
When the function completes without returning anything, it implicitly returns None. Your function has no return statement, so print countdown(n-1) will print None.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With