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)
Related
This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
def fact(n):
if n<0:
return 0
if (n==0 or n==1):
return 1
else:
n * fact(n-1)
n = int(input("Enter a number"))
fact(n)
i dont know why its giving me a TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
You failed to return a value from your recursion case:
else:
n * fact(n-1)
You need
else:
return n * fact(n-1)
The default return value is None, so you end up trying to multiply None by n at every level above the second from the bottom.
For instance, when you try fact(3), you recur ...
3 * fact(2)
2 * fact(1)
fact(1) is 1 ... return that ...
2 * 1 is 2 ... but you then return None ...
3 * None ... is illegal
I believe you meant
return n * fact(n-1)
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:
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))
EDIT: I know I can import factorials but I'm doing this as an exercise
Trying to get the factor of a given number with a function in Python.
For example:
factorial(4) = 4 * 3 * 2 * 1 = 24
def factorial(x):
n = x
while n >= 0:
x = n * (n - 1)
n -= 1
return x
try like this: to make your code work
def factorial(x):
n = 1 # this will store the factorial value
while x > 0:
n = n*x
x -= 1
return n
you got many advice on comments follow it
A good way of approaching this would be using recursion where a function calls itself. See Function for Factorial in Python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
But in your case your return statement actually breaks out of the while loop. So if you pass in 5 you get 20 back which is obviously not 5! (5 factorial).
Instead, try this:
def factorial(x):
n = 1
while x > 1:
n *= x
x -= 1
return n
print (factorial(5))
But do have a go at the recursive approach.
If you really want to cheat try:
import math
math.factorial(n)
I present an even shorter code for recursive factorial calculation. Not sure if it is faster or slower than other approaches:
def fac(n):
return 1 if n==1 else n*fac(n-1)
fac(10)
3628800
def factorial(n):
total = 1
for num in range(2,n+1):
total *= num
return total
input:
n = 10
print(str(n) + "! = ", end = '')
def factorial(n):
'''
print factorial number in human way
'''
if n < 0:
return 'factorial() not defined for negative values'
if n == 0:
return 1
if n == 1:
print('', n, ' =', end = ' ')
return 1
else:
print('', n, '*', end = '')
return n * factorial(n - 1)
print(factorial(n))
output:
10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800
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)