Reputation: 55
def recursive_factorial(n):
if n == 1: #base case
return 1
else:
return n * recursive_factorial(n-1) #recursive call
pls help I am getting a runtime error:RuntimeError('maximum recursion depth exceeded'
Upvotes: 1
Views: 582
Reputation: 5482
So, you have reached your recursion limit. This can be reset by importing sys and setting but:
Seriously don't use setrecursionlimit
You definitely try to iterate before using recursion. Please try this, which should work if you cannot set recursion limits:
re(n):
g=n
while n>1:
g*=(n-1)
n-=1
return g
If you really, really want to set your recursion limit, make sure that you do so only temporarily. Otherwise other, heavier, functions may create issues if too recursive:
import sys
def thing_i_want_to_recurse(n)
c_limit = sys.getrecursionlimit()
sys.setrecurionlimit(n)
yield
sys.setrecurionlimit(c_limit)
Again iteration is best:
[in]: sys.setrecursionlimit(3)
[in]: recursive_factorial(5)
[out]:Error: maximum recusion depth exceeded
[in]: re(5) #no problem
[out]: 120
Upvotes: 1