Python

RecursionError: maximum recursion depth exceeded

RecursionError

A function called itself too many times. Python's default limit is around 1000.

Common Causes

Fixes

  1. 1. Add base case
    def fib(n):
        if n <= 1: return n
        return fib(n-1) + fib(n-2)
  2. 2. Use iteration instead
    def fib(n):
        a, b = 0, 1
        for _ in range(n): a, b = b, a+b
        return a
  3. 3. Increase limit (rare)
    import sys
    sys.setrecursionlimit(2000)

Still not fixed?

Related Errors

DuskTools That Might Help

All Errors