7. Reverse


#1



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

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):
    rev=[]
    l=len(text)-1
    i=0
    while l>=0:
        rev[l]=text[i]
        l=l-1
        i=i+1
        return "".join(rev)


#2

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:

l=len(text)-1

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

rev[l]=text[i]

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


#3

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')

#4

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


#5

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