7. Reverse/ What's the difference?


#1



7.


Replace this line with your code. 

Correct Code

def reverse(text) : 
    result = ''
    a = len(text) - 1
    while a >= 0 :
        result += text(a)
        a -= 1
    return result


Incorrect Code

def reverse(text) :
    result = ''
    while len(text) > 0 :
        result += text(len(text)-1)
        len(text) -= 1
    return result

"Error Message!

Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!"



What's the difference btw them??


#2

len(text) and len(text) -1 is a huge difference. try:

while len(text) - 1 > 0 :

That should also work


#3

Doesn't work

def reverse(text) :
    result = ''
    while len(text) - 1 > 0 :
        result += text(len(text)-1)
        len(text) -= 1
    return result

File "python", line 5
SyntaxError: can't assign to function call


#4

forget my first answer, this is a huge problem:

len(text) -= 1

you can't do this, len() is a function calculating the length of text, you can see this if you remove the shorthand:

len(text) = len(text) - 1

you want to let len recalculate the length minus one? That doesn't work, don't forget that len() is a function call.