0
Please help me, I need my code free of Recursion error
def reversed(x): string=reversed(x) return string a='asdf' print(reversed(a))
6 Respuestas
+ 5
Kevin Ngigi ,
to get help, you need to share some more information with us.
▪︎please give us a brief description of the task that has to be done
▪︎also give a sample of input and output dara that is expected
i would also recommend you to work through the python tutorial, there is a sample where recursion is demonstrated.
thanks!
+ 4
def reversed(x):
string= x[::-1]
return string
a='asdf'
print(reversed(a))
+ 3
dont call your own function inside of itself.
you made a function called reversed and the first thing you do is call the same function again.
it will keep doing that forever
+ 2
What are you trying to achieve by calling function again and again ?
+ 2
def reversed1(x):
text = ''.join(reversed(x))
return text
a='asdf'
print(reversed1(a))
0
you doesn't need recursion to achieve that...
however, if you absolutly want to use recursion:
def reversed(st):
if len(st)<2: return st
return st[-1]+reversed(st[:-1])
print(reversed(input()))
but it's shorter and more efficient to do:
print(input()[::-1])