This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
def fact(n, summ):
if n == 0:
print(summ) -- Prints 55
return summ
fact(n-1, summ + n)
print(fact(10, 0)) -- Output None
You need to return fact(n-1, summ + n) as a returning value. If a function does not return a value then it defaults to returning None.
def fact(n, summ):
if n == 0:
return summ
return fact(n-1, summ + n)
print(fact(10, 0))
This outputs:
55
On a side note, your fact function could be re-implemented without the second parameter. The following produces the same output:
def fact(n):
if n == 0:
return 0
return n + fact(n-1)
print(fact(10))
Related
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 11 months ago.
I study recursive_function.
I think It have to print 120 ( 5 * * 4 * 3 * 2 * 1 )
but, It print 'None'
j = 1
def factorial(n):
global j
j = n * j
n = n -1
if n == 0:
return j
else:
factorial(n)
print(factorial(5))
You have to return when you make your recursive call. Rather than
factorial(n)
consider
return factorial(n)
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
Trying to build a recursive def that will counts how many times the number '7' occurs in a given number. I dont understand why my return keeps giving me None.
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
helpcount7(NewN,x)
else:
print(x)
return helpcount7(N,x)
print(count7(717))
for example, if I input 717 my answer would be:
2
None
The reason is because you are not returning anything from your helpcount7() function. You should return something from it, for example:
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
return helpcount7(NewN,x)
else:
return x
return helpcount7(N,x)
print(count7(717))
This question already has answers here:
Recursive function does not return specified value
(2 answers)
Closed 7 years ago.
I did factorial using tail recursion, it is returning "none" - why
def fact(n, k):
print "n = %d k = %d" % (n,k)
if n == 1:
print "k final = ", k
return k
else:
# print n
print k
fact(n-1, k*(n-1) )
a =(fact(4, 4) )
print a
You haven't returned a value in the else clause -
else:
fact(n-1, k*(n-1) )
This should read
else:
return fact(n-1, k*(n-1) )
This question already has answers here:
Python function returns None (all trivial solutions checked and they do not work) [duplicate]
(1 answer)
Weird function return value? [duplicate]
(3 answers)
Closed 9 years ago.
count = []
def problem14(n):
count.append(n)
if n == 1:
return count
if n % 2 == 0:
n = n/2
problem14(n)
else:
n = 3*n + 1
problem14(n)
print problem14(13)
So this is code that I have written. I have no idea why it's returning None while in my opinion it should return list 'count'. Any help?
You still need a return statement when using recursion, otherwise the return value will be lost:
def problem14(n):
count.append(n)
if n == 1:
return count
if n % 2 == 0:
n = n/2
return problem14(n) # <--
else:
n = 3*n + 1
return problem14(n) # <--
By the way, this is probably the wrong approach for Project Euler #14 :-) Consider using a dynamic programming approach instead (that's all I'll say so as to not ruin the fun).
You should use the return keyword in order to return a value from a function.
return problem14(n)
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed last year.
def recursiveadd(x,sum1):
if x > 0:
sum1 += x
recursiveadd(x-1,sum1)
else:
return sum1
print recursiveadd(100,0)
Inserting a "print sum1" after the addition shows that sum1 is being increased, so I don't understand why the function returns None. The only thing I can think of is that sum1 is somehow being reset to 0 before being returned, but I have no idea why that would be.
You need to write
def recursiveadd(x,sum1):
if x > 0:
sum1 += x
return recursiveadd(x-1,sum1)
else:
return sum1
print recursiveadd(100,0)
In [52]: def rec(x, sum = None):
if sum == None:
sum = 0
if x > 0:
sum += x
return rec(x - 1, sum)
else:
return sum
....:
In [53]: rec(100)
Out[53]: 5050