RecursionError: maximum recursion depth exceeded
Quick summary
Your recursive function never stops calling itself — Python hits the default recursion depth limit (usually 1000) and raises RecursionError.
Why this happens
The base case that stops recursion is missing or unreachable for the given input values
The recursive step does not reduce the problem size, so the function never converges toward the base case
The recursion depth genuinely exceeds sys.getrecursionlimit() for very deep but correct recursion
Minimal example
def countdown(n):
print(n)
countdown(n - 1) # no base case!
countdown(10) # RecursionError after ~1000 callsdef countdown(n):
if n <= 0: # base case
return
print(n)
countdown(n - 1)
countdown(10) # prints 10..1, then stopsAdding the base case (n <= 0) gives the recursion a termination condition, so it stops calling itself and returns before exceeding the depth limit.
How to diagnose
Add a print or logging statement at the function entry to trace the argument values across recursive calls
Check whether the base case condition can ever be true for the inputs you are passing
Run sys.getrecursionlimit() to see the current limit and compare with the expected recursion depth
How to fix
Add or fix the base case so the function returns before recursing when the input reaches the terminal condition
Ensure the recursive call passes a strictly smaller or simpler argument that moves toward the base case
For legitimately deep recursion, increase the limit with sys.setrecursionlimit() or rewrite the function iteratively
How to prevent
Always write and test the base case before the recursive case when designing recursive functions
Prefer iterative solutions over recursive ones for problems with potentially large input sizes