Please help with reverse


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!


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?


Yes, thanks it's working now :slight_smile:


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


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


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