7. Reverse


Hi everyone! When I run this code it reverses the string when using print to check it, but when i change print to return, Codecademy claims that the code is not returning the reversed string. Codecademy claims the code is only returning the first character of the reversed string, in this case "!". Even though I see that it works before.

Any ideas on what is causing the problem, I really don't see where I could go from here.

Thanks in advance!

def reverse(text):
    i = len(text)
    while i > 0:
        return text[i-1]
        i -= 1


Hi, @domosobo ,

The return command terminates execution of a function and returns a value to the calling statement. Therefore, your code only executes the first iteration of the while loop.

Instead of what you have now, you should initialize an empty list prior to the while loop.

Within the while loop, append each character to the list.

After the loop, call the join method from an empty string, passing the join method the list of characters, and return the result of that operation.

Post your revised code if you encounter any problems with it.


Alright, yeah I understand what the issue was now. Your explanation cleared things up. Thanks a ton!