7 Reverse


#1


My code appears to reverse the string, but I am getting an error message shown below. Does anyone know what I should change?



I get the following error message:
Oops, try again. Your function fails on reverse("Python!"). It returns "!nohtyPcba!nohtyP" when it should return "!nohtyP".


I was expecting this to work, and the output window does show that the string gets reversed. This is the output:
!nohtyP
None


ans=[]

def reverse(text):
    pos = -1
    for i in range(len(text)):
        ans.append(text[pos])
        pos = pos -1
    return ''.join(ans)
print reverse("Python!")


#2

place this:

ans=[]

inside your function

the exercise calls the function multiply times for validation, if ans is not inside the function, the list doesn't get reset


#3

Thank you. That fixes it. :grinning:


#4