RecursionError in Python: Causes and Fixes
RecursionError happens when a function keeps calling itself too many times.
In Python, recursion means a function calls itself to solve a smaller version of the same problem. This can be useful, but the function must have a clear way to stop. If it never stops, or if it goes too deep, Python raises RecursionError.
Quick fix #
def countdown(n):
if n <= 0: # base case
return
print(n)
countdown(n - 1)
countdown(5)
Output:
5
4
3
2
1
Most RecursionError problems happen because the function has no base case or the base case is never reached.
What this error means #
- Recursion happens when a function calls itself.
- Python stops very deep recursion and raises
RecursionError. - This usually means the function keeps calling itself too many times.
A common error message looks like this:
RecursionError: maximum recursion depth exceeded
If you are new to functions, see Python functions explained first.
Why it happens #
This error usually happens for one of these reasons:
- There is no base case to stop the recursion.
- The base case exists but the code never reaches it.
- The recursive step does not move toward the stopping condition.
- The input is too large for a recursive solution in Python.
If you want a simple definition of recursion, read what recursion is in Python.
Example that causes the error #
Here is a minimal example that causes RecursionError:
def greet():
print("Hello")
greet()
greet()
Why this fails #
The line below keeps repeating forever:
greet()
The function calls itself, then calls itself again, then again, with no stopping condition.
After enough calls, Python stops the program and raises RecursionError.
Fix 1: Add a base case #
A base case is the condition that stops the function.
Put the base case before the recursive call, and make sure it returns or exits.
Broken version #
def count_up(n):
print(n)
count_up(n + 1)
count_up(1)
This never stops.
Fixed version #
def count_up(n):
if n > 5: # base case
return
print(n)
count_up(n + 1)
count_up(1)
Output:
1
2
3
4
5
The important part is that the function checks the stopping condition before making the next recursive call.
Fix 2: Move toward the base case #
Even if you have a base case, the function still fails if the values do not move toward it.
Wrong direction #
def countdown(n):
if n == 0:
return
print(n)
countdown(n + 1)
countdown(3)
This has a base case, but n moves away from 0 instead of toward it.
Correct version #
def countdown(n):
if n == 0:
return
print(n)
countdown(n - 1)
countdown(3)
Output:
3
2
1
When writing recursive code:
- Change the argument on each call.
- Make the problem smaller each time.
- Check that the value really gets closer to the stopping condition.
Fix 3: Use a loop instead #
Some tasks are easier with a for loop or while loop.
Loops do not build up recursive calls in the same way, so they avoid deep recursion problems.
Recursive version #
def countdown(n):
if n <= 0:
return
print(n)
countdown(n - 1)
countdown(5)
Loop version #
n = 5
while n > 0:
print(n)
n -= 1
Output:
5
4
3
2
1
If recursion is not necessary, iteration is often simpler. You can learn more in Python while loops explained.
Debugging checklist #
If your recursive function is failing, try this checklist:
- Print the function argument before the recursive call.
- Check whether the value changes on each call.
- Confirm the base case can actually become true.
- Test with a very small input first.
Useful debugging examples:
def countdown(n):
print("calling with:", n)
if n <= 0:
return
countdown(n - 1)
countdown(3)
Output:
calling with: 3
calling with: 2
calling with: 1
calling with: 0
You can also inspect Python’s current recursion limit:
import sys
print(sys.getrecursionlimit())
If you are still stuck, see this beginner guide to debugging Python code.
About increasing the recursion limit #
Python has a recursion limit to protect your program from crashing due to infinite or very deep recursion.
You can change the limit with sys.setrecursionlimit(), like this:
import sys
sys.setrecursionlimit(2000)
print(sys.getrecursionlimit())
But this is usually not the first fix.
For beginners, the better solution is normally to fix the recursive logic:
- Add a base case
- Make sure the function moves toward that base case
- Use a loop if recursion is not needed
Raising the limit can hide a bug instead of solving it.
Common mistakes #
These are the most common causes of RecursionError:
- Missing base case
- Wrong comparison in the base case
- Recursive call uses the same value every time
- Value changes in the wrong direction
- Using recursion for very large input
For a broader overview of Python exceptions, read Python errors and exceptions explained.
FAQ #
What is a base case in recursion? #
It is the condition that stops the function from calling itself again.
Can I fix RecursionError by increasing the recursion limit? #
Sometimes, but usually the better fix is to correct the recursive logic or use a loop.
Why does my recursive function never stop? #
Usually because the stopping condition is missing, incorrect, or never reached.
Is recursion bad in Python? #
No, but Python is not ideal for very deep recursion. Simple recursion is fine when the stopping condition is clear.