Python Forum

Full Version: can you understand why this code prints None?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# assuming n is always greater than m
def fd(n,m):
    if n%m==0:
        print(m)
        return m
    else:
        print(n,m)
        fd(n,(m-1))
print(fd(6,5))
the result is not what i expect,i expect
Output:
6,5 6,4 3 3
but the result i get is:
Output:
6,5 6,4 3 None
i don't why this None is occurring??
If n % m == 0, then there is a return statement. But if n % m != 0, there is no return statement. In that case, the default return of None is done by Python. You probably want line 8 to be:

return fd(n, m - 1)   # you don't need parentheses for m - 1.
you are awesome,thanks.

(Mar-13-2019, 02:53 AM)ichabod801 Wrote: [ -> ]If n % m == 0, then there is a return statement. But if n % m != 0, there is no return statement. In that case, the default return of None is done by Python. You probably want line 8 to be:
 return fd(n, m - 1) # you don't need parentheses for m - 1. 


you are the best, thanks.