ProjectStack
python

RecursionError: maximum recursion depth exceeded

A recursive function called itself too many times without reaching a stopping condition. Python's default recursion limit is 1000 calls, after which it raises this error to prevent a stack overflow.

Common causes

  • A recursive function is missing its base case (the condition that stops the recursion)
  • The base case is present but is never reached due to a logic error
  • The input data is deeply nested beyond the recursion limit
  • Two functions calling each other infinitely (mutual recursion with no exit)

How to fix it

  1. Add or fix the base case in your recursive function — it must be reachable
  2. Add a print/debug at the top to trace how deep calls go: print(depth, value)
  3. Convert deep recursion to iteration using an explicit stack (list used as LIFO)
  4. As a last resort, increase the limit: import sys; sys.setrecursionlimit(5000) — but fix the root cause first

Example

Traceback (most recent call last): File "app.py", line 3, in countdown countdown(n - 1) [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded

A countdown function called with countdown(-1) where the base case only stops at n == 0

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.