Oops, try again. Your function fails on reverse("Python!"). It returns "Python!" when it should return "!nohtyP".

def reverse(text):
    t = ""
    for s in text:
        t = t + s
    return t


Here's how I did it:

def reverse(text):
    n = "" #Create's an empty list.. you got this!
    index = len(text) #Variable called "index" which is set to the length of the text entered
    for i in range(0, index): #For every letter from 0 to "index"
        n = n + text[(index-1)-i] 
    return n 

print reverse('George!')

n = n + "adds onto the empty list with the list included"
text[(index-1)-i] "Iterates over an over, till the word's over.. Its a bit self-explanatory"

Hope this helps you out! :smiley:


Thanks it really helped! The only thing that i dont understand is why you did (index -1) and then (- i)


My understanding is this:
Because index (not to be confused with your variable index here) starts from 0, not from 1. So to get to the last index of a word, you have to deduct 1 from the length of the word.

Also to reverse the order of the characters, i has be to deducted (- i) from the last index in each loop.


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