Reverse understing


Hello, I am wondering why I need the while statement in my code for it to work. Without the while statement it will only reverse like 2 or 3 letters from the list, then stop. I just don't understand what it is doing. With the while statement it appears to run correctly and passes.

lst = []
def reverse(text):
    y = ""
    for n in text:
    while len(lst) > 0:
        for x in lst:
            y = y + lst[len(lst)-1]
            del lst[len(lst)-1]
    print lst
    print y
    return y



I am receiving this message for my code:

Your function fails on reverse("Python!"). It returns "!nohtyPcba" when it should return "!nohtyP".

temp = []

def reverse(text):
    text = str(text)
    for i in text:
        temp.insert(0, i)
    print temp
    return (''.join(temp))

I don`t understand where the letters a, b, and c come frome. As far as I can see, I haven't used the letter a even once in this code :slight_smile:

Could someone explain why the letters appear and what mistake I've done?


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