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

7th Apr 2020, 5:53 PM
Kath
Kath - avatar
4 Antworten
+ 3
This is not recursion, this is an endless loop. Recursion needs an exit condition. https://images.app.goo.gl/tBwmxbSiAjEQAGL48
7th Apr 2020, 6:12 PM
Tibor Santa
Tibor Santa - avatar
+ 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.
7th Apr 2020, 10:45 PM
GeraltdeRivia
+ 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
8th Apr 2020, 7:23 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
7th Apr 2020, 7:30 PM
MO ELomari
MO ELomari - avatar