I get this 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!

def reverse(text):
    a = len(text)
    while a > 0:
        print text[a],
        a -= 1


This is an issue with your array lengths. Since they're zero-based, setting a to be equal to the length of the array causes issues. If your array had 4 items in it, A would be equal to 4. When you try to access text[a], it's looking at text[4], which doesn't exist.

The easiest solution to this would be to change the second line to a = len(text) - 1 so that it checks all the indices properly


Thanks for your reply! I tried that and now I get this error:

Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP".


Did you remember to call the function by using reverse("Python!") at the end? I just ran the code and it seems to be working perfectly fine


weird, I did, and it still gives me an error:


I ran the following code over at labs

def reverse(text):
    a = len(text) -1
    while a >= 0:
        print text[a],
        a -= 1

which then returned ! n o h t y P as it’s output. I also needed to change the loop from > to >= to get it to properly return the P. What error are you getting?

I haven’t been able to test it in the actual lesson, since I haven’t unlocked it yet


It prints the following with the same error message:

! n o h t y PNone


alright, I just tested it in my external editor, and there it works perfectly fine. Seems to be an error with the lesson, not an error on my part. Thanks a lot for your help!


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