0
maximum recursion depth exceeded
def reverse(x): reverse("x") print(reverse("asddf")) i dont understand why it gives back the error in the title sorry, for that stupid question
4 Answers
+ 3
This is not recursion, this is an endless loop.
Recursion needs an exit condition.
https://images.app.goo.gl/tBwmxbSiAjEQAGL48
+ 3
Tibor Santa I disagree. It is recursion wrongly executed. It becomes an endless loop. Recursion is a function calling itself. Obviously when using recursion you always need an exit otherwise you end in an endless loop like this case.
+ 3
GeraltdeRivia ok, you are right of course ;)
Kath have you figured out yet?
There is actually a very easy way to reverse a string that doesn't involve recursion, only list slicing:
text = 'hello'
print(text[::-1]) # olleh
+ 1
the underlying problem is that there's nothing blocking it from recursion ( it keeps calling itself until it exceeds its limit ).
so one way to fix this is that you have to add what's called the base case.