Reverse


#1


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

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


#2

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:


#3

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


#4

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.


#5

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