7.reverse


#1

can anyone explain why it says i through a "local variable 'e' referenced before assignment"

def reverse(text):
    x = str(text)
    y = len(x)
    z = y-1
    word = ""
    while e > 0:
        word += text[e]
        e -= 1
    return word

#2

here:

while e > 0:

you use e, but you haven't defined it yet, where do you think you created e?


#3

i don't know i posted my code up there in it's entirety


#4

well, you can't use variables (like you do in your loop condition) before you defined them. Sure you didn't mean to use z? it contains the length of text minus one, which would be perfect


#5

thx but now it says Your function fails on reverse("Python!"). It returns "!nohty" when it should return "!nohtyP". after i replaced all the e's with z's


#6

well, now your loop condition is this:

while z > 0:

the problem is, strings are zero indexed based. so the p of python is index 0, your loop ends at index 1. So you need to slightly modify your loop condition


#7

Also: You don't need "x = str(text)."
I tried your code, with the correction to the loop condition, and "y = len(text)" works just as fine.

It's already a string that you recieve, as stated in the instructions.


#8

thx that solved the problem


#9