Python Forum
can you understand why this code prints None? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: can you understand why this code prints None? (/thread-16744.html)



can you understand why this code prints None? - arcbal - Mar-13-2019

# 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??


RE: can you understand this code? - ichabod801 - Mar-13-2019

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.



RE: can you understand this code? - arcbal - Mar-13-2019

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.