How to fix this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to fix this

I want to create a program which reverses a string using recursion. Why the error is happening and how to fix it? https://code.sololearn.com/cX8UuyP6a5u8/?ref=app

26th May 2020, 9:27 AM
Anonymous Person
Anonymous Person - avatar
2 Answers
+ 4
Anonymous Person On line number 6 you have passed number as parameter which doesn't has len() rev(n-1) //wrong Change rev(n - 1) to rev(string[:n]) https://code.sololearn.com/ccf9QK7HSJwb/?ref=app
26th May 2020, 9:31 AM
AอขJ
AอขJ - avatar
0
Here is the simple way, def rev(string): if len(string) == 0: return "" return (string[-1] + rev(string[:-1])) print(rev("news"))
26th May 2020, 10:24 AM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar