18.3 Infinite Recursion

Infinite Recursion

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
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.