maximum recursion depth exceeded | Sololearn: Learn to code for FREE!


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/7/2020 5:53:47 PM


4 Answers

New Answer


This is not recursion, this is an endless loop. Recursion needs an exit condition.


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.


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


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.