When working with recursion, you will likely run into this problem more than once, especially if you are new to the concept.
Infinite recursion occurs when a piece of code constantly repeats itself, without the ability to stop and move on. Preventing infinite recursions from happening is fairly simple. The only thing you need to do is make sure to add a reachable base case when working with recursion. If, however, you do end up running into infinite recursion, a
RecursionError will eventually be raised.
# notice how there is no base # case, meaning no way out def recurse(i): i = i + 1 print(i) recurse(i) recurse(0)
View code on GitHub.
... 994 995 996 RecursionError: maximum recursion depth exceeded while calling a Python object