I had an exercise and tried to use parts of code I found here on another person's question, but I found that I needed a part of code that I have no idea why I do.
The full code I was using for my function is this:
def rreverse(s):
if s == "":
return s
else:
return rreverse(s[1:]) + s[0]
But I only used the else as a statement, and I didn't get the result I was hoping for.
def recur_reverse(x):
if x != "":
return recur_reverse(x[1:]) + x[0]
I got TypeError saying "unsupported operand type(s) for +: 'NoneType' and 'str'."
What is the logic behind this first example working fine and the second one throwing an error when the difference is this if statement? and why is my version incorrect?
Thank you!