No modern computer is going to combust due to user code, I promise. Try it for yourself:
def recurseInfinitely(n):
recurseInfinitely(n + 1)
recurseInfinitely(0)
Running this program promptly yields the output:
[...]
recurseInfinitely(n + 1)
File "so.py", line 2, in recurseInfinitely
recurseInfinitely(n + 1)
File "so.py", line 2, in recurseInfinitely
recurseInfinitely(n + 1)
RuntimeError: maximum recursion depth exceeded
You can catch the error and check the value of n
:
def recurseInfinitely(n):
try:
recurseInfinitely(n + 1)
except RuntimeError:
print "We got to level %s before hitting the recursion limit." % n
Last Updated : 23 Jul, 2021,GATE CS 2021 Syllabus,GATE Live Course 2023
5 4 3 2 1
The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack overflows.,By default, the maximum depth of recursion is 1000. If the limit is crossed, it results in RecursionError. Let's look at one such condition.,Each function multiplies the number with the factorial of the number below it until it is equal to one. This recursive call can be explained in the following steps.,Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.
Example of a recursive function
def factorial(x): "" "This is a recursive function to find the factorial of an integer "" " if x == 1: return 1 else: return (x * factorial(x - 1)) num = 3 print("The factorial of", num, "is", factorial(num))
Output
The factorial of 3 is 6
Each function multiplies the number with the factorial of the number below it until it is equal to one. This recursive call can be explained in the following steps.
factorial(3) # 1 st call with 3 3 * factorial(2) # 2n d call with 2 3 * 2 * factorial(1) # 3 rd call with 1 3 * 2 * 1 # return from 3 rd call as number = 1 3 * 2 # return from 2n d call 6 # return from 1 st call
Most of the time, infinite recursion causes the program to run for a while and then produce a Maximum recursion depth exceeded error.,Most of the time, an infinite recursion will cause the program to run for a while and then produce a “RuntimeError: Maximum recursion depth exceeded” error. If that happens, go to the “Infinite Recursion” section below.If you are not getting this error but you suspect there is a problem with a recursive method or function, you can still use the techniques in the “Infinite Recursion” section.,Runtime errors are produced by the interpreter if something goes wrong while the program is running. Most runtime error messages include information about where the error occurred and what functions were executing. Example: An infinite recursion eventually causes the runtime error “maximum recursion depth exceeded”. ,The first step is to examine the place in the program where the error occurred and see if you can figure out what happened. These are some of the most common runtime errors:
For example:
while x > 0 and y < 0:
# do something to x
# do something to y
print('x: ', x)
print('y: ', y)
print("condition: ", (x > 0 and y < 0))
This can be rewritten as:
neighbor = self.findNeighbor(i) pickedCard = self.hands[neighbor].popCard() self.hands[i].addCard(pickedCard)
Another problem that can occur with big expressions is that the order of evaluation may not be what you expect. For example, if you are translating the expression x/2 π into Python, you might write:
y = x / 2 * math.pi
If you have a return statement with a complex expression, you don’t have a chance to print the result before returning. Again, you can use a temporary variable. For example, instead of:
return self.hands[i].removeMatches()
you could write:
count = self.hands[i].removeMatches()
return count
Tail recursion is considered a bad practice in Python, since the Python compiler does not handle optimization for tail recursive calls. The recursive solution in cases like this use more system resources than the equivalent iterative solution.,Any computation that can be made using iteration can also be made using recursion. Here is a version of find_max written using tail recursion:,The two examples above each have a base case which does not lead to a recursive call: the case where the element is a number and not a list. Without a base case, you have infinite recursion, and your program will not work. Python stops after reaching a maximum recursion depth and returns a runtime error.,Modern programming languages generally support recursion, which means that functions can call themselves within their definitions. Thanks to recursion, the Python code needed to sum the values of a nested number list is surprisingly short:
>>> tup = 2, 4, 6, 8, 10
>>> tup = (2, 4, 6, 8, 10)
>>> tup = (5,)
>>> type(tup)
<type 'tuple'>
>>> tup = (5)
>>> type(tup)
<type 'int'>
>>> tup = ('a', 'b', 'c', 'd', 'e') >>>
tup[0]
'a'
>>> tup[1: 3]
('b', 'c')