Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is 'None' print out in my python code [duplicate]

Tags:

python

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.

like image 958
pinseng Avatar asked Aug 11 '26 11:08

pinseng


1 Answers

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.

like image 187
TigerhawkT3 Avatar answered Aug 13 '26 01:08

TigerhawkT3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!