Error guide · Other errors
RecursionError: maximum recursion depth exceeded
A function called itself too many times without stopping.
Example
def countdown(n): print(n) countdown(n - 1) countdown(3)
What Python says
RecursionError: maximum recursion depth exceeded
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
A recursive function needs a base case: a condition where it stops calling itself. Without one, each call starts another call, until Python runs out of room.
How to fix it
def countdown(n): if n == 0: return print(n) countdown(n - 1) countdown(3)
Output: 3 ⏎ 2 ⏎ 1
Tips
- Every recursive function needs an
ifthat returns without calling itself. - Check that each call moves closer to the base case.
- For simple repetition, a loop is often clearer than recursion.