I expect True, but get No

I have a simple Python script that recursively checks if number ranges are number nfactors x. If any of the numbers is not a factor, I return False, otherwise, when n==1I would like to return True. However, I keep coming back NoneTypeand I will be grateful for suggestions on how to fix this.

#Function
def recursive_factor_test(x, n):
    if n==1:
        return True
    else: 
        if  x % n == 0:
            #print "passed {}".format(n)
            recursive_factor_test(x,n-1)
        else:
            return False

#Example Expecting False
print recursive_factor_test(5041,7)
>>False
#Example Expecting True
print recursive_factor_test(5040,7)
>>None
type(recursive_factor_test(5040,7))
>>NoneType
+5
source share
1 answer

You never return the return value of a recursive call:

if  x % n == 0:
    #print "passed {}".format(n)
    return recursive_factor_test(x,n-1)

If you omit the statement return, your function ends without a return statement, returning to the original Nonedefault value .

In returnthere it works:

>>> print recursive_factor_test(5040,7)
True
+10

All Articles