Reverse


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#


ERROR: Oops, try again. Your function fails on reverse("coolstory"). It returns "yroootslc" when it should return "yrotslooc".


Hello smart people,

If I reverse word "Python!" it correctly switches to "!nohtyP" or "abc" to "cba". However I fail to see why "Coolstory" changes the order in the list new_text, because I've read that order of items in a list should be persistant.

Am I missing something? Or whole code is totally wrong. Idk. Can anybody help?


def reverse(text):
    x = len(text)
    text = list(text)
    new_text = []
    while x >= 1:
        new_text.append(text[x - 1])
        text.remove(text[x - 1])
        x -= 1
    return ''.join(new_text)


#2

the problem with your code is that remove() will remove the first match it can find. This makes it really difficult

I recommend a different approach


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.