#7 reverse


#1



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

Oops, try again. Your function fails on reverse("Python!"). It returns "[7, 6, 5, 4, 3, 2, 1]" when it should return "!nohtyP".

My thought on solving this exercise was to make a list using the string "text". In this case i take txt and make it a list using the data from text, then I use range() to work from the last value text and work my way backwards from l to 0. As I work through the list backwards in the for loop I append the value to the flip list, then I return flip. However I get numbers (indexes), not the actual letters. What am I doing wrong here?

Thanks in advanced.


def reverse(text):
    l = len(text)
    txt = list(text)
    flip = []
    for x in range(l,0,-1):
        flip.append(x)
    return(flip)


#2

@zkhudson: Here's the problem :

flip.append(x)


Instead of appending the value on that position, whose index is being held by the variable x, you're just storing the index of it.

listTest = [4,5,6]
for element in range(3):
    print "Index %d" % (element) #Brings back the index
    print "Value %d at position %d" % (element, listTest[element]) #Brings back the number on that position

On my second print, since I have a list, I can access a valid position* by doing:

print listName[indexVariable]

Hope my explanations helps you!
If not, let us know!

Note: valid position stands for 0 until N - 1, where N is the size of your list. So if you have a list with 10 elements, if you try to print the element at position ten, print listTest[10], it will be incorrect due to the fact in programming we start counting from 0 and due to that, our list goes from 0 to 9.