7. Reverse



Why does this code not work in Python, I am not able to understand what is wrong with this
Error Message:
Oops, try again. Your code looks a bit off--it threw a "list assignment index out of range" error. Check the Hint if you need help!

def reverse(text):
    while l>=0:
        return "".join(rev)


i would put return at the same indent level as your first loop, otherwise the loop will run only once.

Uhm.... here you create l:


so l is a high number, lets say 8. then you try to access index 8 at a empty list:


rev[8] on a empty list, that is going to through a index error

You might want to add a function call, then you can add print statements to see what is actually happening, that is making it so much easier to build such a program


hello,what's problem with my code

def reverse(text):
    s = ''
    for i in range(len(text)-1,-1):
        s += text[i]
    return s
print reverse('abcd')


if you want to make steps of minus one, your range function need a third argument, the step size. see documentation


step is -1,needed to add.default step is 1 .OK ,thank you,got it.