Please help with reverse


#1



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


My code returns the error message 'Oops, try again. Did you create a function called reverse? Your code threw a "local variable 'result' referenced before assignment" error.'


def reverse(text):
    position = (len(text)-1)
    for i in text:
        while position >= 0:
            result = result + text[position]
            position -= 1
    return result

Thank you!


#2

You forgot to define your result string so python don't know what to do. when you try to use result variable it throws an error.

It should be an empty string right?


#3

Yes, thanks it's working now :slight_smile:


#4

why do you have a for loop? It doesn't do anything at all, the reversing is done purely by your while loop


#5

Good point, I think it was left in from something I was trying before


#6

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