#7 reverse



<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. Your function fails on reverse(“Python!”). It returns “[7, 6, 5, 4, 3, 2, 1]” when it should return “!nohtyP”.
<What do you expect to happen instead?>
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):

<do not remove the three backticks above>


@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.