What does max recursion depth exceeded mean?


#1

Question

What does max recursion depth exceeded mean?

Answer

If you receive the error maximum recursion depth exceeded, you likely need to rework your cube function. To cube a number, we’re simply multiplying it by itself 3 times. In Python, we’ve seen that we can use ** to raise something to a power. If we wanted to square a number, we could type number ** 2 or number * number. The same logic applies.
Writing return cube(number) is invalid because it calls the cube function, which then calls the cube function, which then… you get the point. It’s an infinite loop of calling itself!


FAQ: Learn Python - Functions - Practice Makes Perfect
#2

i am still not get the point of that …

could u explain in detail with explicit example code ?


#3

The mentioned error seems to appear when an infinite loop is detected, such as calling a function into itself
ex:
def cube(number):
return cube(number)
print cube(2)

What you are Supposed to do in this exercise is cube the ‘number’ argument (meaning multiply it by itself 3 times), easiest way to do this is to raise it to the power of 3:

def cube(number);
return number ** 3

This way by writing print cube(2) you will get the cube root of 2 - although calling for print is not required in the exercise. (apologies for the lack of indentations, they seem to not show for some reason)


#4

Thx for your explanation .
got better understanding now