RecursionError: maximum recursion depth exceeded (Fix)

RecursionError: maximum recursion depth exceeded means Python stopped your program because a function called itself too many times.

This usually happens with recursive functions. A recursive function is a function that calls itself. Python allows recursion, but it also has a safety limit so your program does not keep running forever and using too much memory.

If you want the quick fix first: check that your function has a base case, and make sure every recursive call moves toward that stopping point.

Quick fix

def count_down(n):
    if n <= 0:  # base case
        return
    print(n)
    count_down(n - 1)

count_down(5)

Output:

5
4
3
2
1

Why this works:

  • if n <= 0: is the base case
  • It stops the recursion
  • count_down(n - 1) moves n closer to 0

Most RecursionError problems happen because:

  • the function has no base case
  • the base case is never reached
  • each call creates more calls than expected

What this error means

Python raises this error when a function keeps calling itself until it hits Python's recursion limit.

In simple terms:

  • Your function did not stop in time
  • Python stopped it for safety
  • The problem is usually in the recursion logic, not in Python itself

If you are new to the idea of recursive functions, see what recursion means in Python first.

When this error happens

This error commonly appears when:

  • A recursive function has no base case
  • The base case exists but is never reached
  • The value changes in the wrong direction
  • Two functions call each other forever
  • The recursion is valid, but the depth is still too large

Here is the main idea:

A recursive function needs two things:

  1. A stopping condition
  2. A change that moves each call toward that stopping condition

Without both, recursion does not end.

Example that causes the error

Here is a small example that fails:

def count_forever(n):
    print(n)
    count_forever(n - 1)

count_forever(5)

This causes a RecursionError because the function has no base case.

It keeps calling itself again and again:

  • count_forever(5)
  • count_forever(4)
  • count_forever(3)
  • and so on...

It never stops, so Python eventually raises the error.

How to fix it

1. Add a base case

A base case is the condition that stops the function.

def count_down(n):
    if n <= 0:
        return
    print(n)
    count_down(n - 1)

count_down(5)

2. Make sure each call gets closer to stopping

A base case is not enough if your value never reaches it.

This version looks close, but it is wrong:

def count_down(n):
    if n <= 0:
        return
    print(n)
    count_down(n + 1)  # wrong direction

count_down(5)

Why it fails:

  • The base case is n <= 0
  • But n keeps increasing
  • So the stopping condition never becomes true

3. Check for recursive calls using the same value

If the function keeps using the same value, it will not make progress.

def count_down(n):
    if n <= 0:
        return
    print(n)
    count_down(n)  # same value again

count_down(5)

This keeps calling count_down(5) forever.

4. Print the value to trace what is happening

A simple print() can help you see whether the function is moving toward the base case.

def count_down(n):
    print("calling with:", n)
    
    if n <= 0:
        return
    
    count_down(n - 1)

count_down(3)

Output:

calling with: 3
calling with: 2
calling with: 1
calling with: 0

This is a fast way to debug recursive code. If you want more general debugging help, see this beginner guide to debugging Python code.

5. Check for accidental circular calls

Sometimes one function does not call itself directly. Instead, two functions call each other forever.

def func_a():
    print("A")
    func_b()

def func_b():
    print("B")
    func_a()

func_a()

This also leads to a RecursionError.

To fix it, make sure one of the functions has a condition that stops the chain of calls.

6. Use a loop when recursion is not necessary

For many beginner tasks, a loop is simpler and safer.

Recursive version:

def count_down(n):
    if n <= 0:
        return
    print(n)
    count_down(n - 1)

count_down(5)

Loop version:

n = 5

while n > 0:
    print(n)
    n -= 1

For simple repetition, a loop is often easier to read and avoids recursion depth problems.

Deep recursion that is technically correct

Sometimes your recursion logic is correct, but the input is so large that Python still reaches the recursion limit.

Example:

def count_down(n):
    if n <= 0:
        return
    count_down(n - 1)

count_down(2000)

This may fail even though the function does stop eventually.

For beginners, the best fix is usually:

  • rewrite the code using a loop
  • avoid very deep recursion unless you truly need it

It is possible to change the recursion limit with sys.setrecursionlimit(), but this is usually not the best first fix.

Why not?

  • It can still fail
  • It can use more memory
  • It may hide a logic error instead of fixing it

If you are curious, Python can show help for that function:

import sys
help(sys.setrecursionlimit)

Use this carefully. In most beginner programs, changing the limit is not necessary.

Beginner debugging steps

If you see this error, use this checklist:

  1. Find the recursive function in the traceback
  2. Identify the base case
  3. Check whether the base case can actually become true
  4. Check whether the argument changes on every call
  5. Test with a very small input first

Useful debug lines:

print(n)
print("calling with:", n)

You can also inspect the traceback:

import traceback

If Python errors and exceptions are still confusing, read Python errors and exceptions explained.

A recursion problem can make your program:

  • slow before it fails
  • print many repeated lines
  • appear stuck

This error is different from other common errors:

  • It is not a SyntaxError, because the code can start running before failing
  • It is not usually a NameError, because the function name normally exists
  • It is a runtime problem caused by how the function behaves while the program runs

If you want to review how functions work in general, see Python functions explained.

Common causes

These are the most common reasons beginners get this error:

  • Missing base case
  • Wrong comparison in the base case
  • Recursive call uses the same value again
  • Recursive call moves away from the stopping condition
  • Mutual recursion between two functions
  • Using recursion for a task better solved with a loop
  • Trying to process very large input with recursion

FAQ

What is recursion in Python?

Recursion is when a function calls itself. It must have a stopping condition called a base case.

Can I fix this by increasing the recursion limit?

Sometimes, but it is usually better to fix the logic or rewrite the code with a loop first.

Why does my base case not work?

The value may never reach the condition, or the comparison may be wrong.

Is recursion bad in Python?

No, but for many beginner tasks a loop is simpler and avoids recursion depth problems.

See also