Write Python. Understand every error.

Open the editor

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.

Try this code in Py-CRAFT

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

Other common errors

Share this page: