# Reverse - Why doesn't this work?

#1

Practice makes perfect - Reverse

There are no error messages. It only prints the middle letter of "cat"

I expected it to produce a list [t, a, c]

``````def reverse(x):
#make sure x is a string
word = str(x)
#counting backwards from the length by -1
for i in range(len(word)-1, 0, -1):
# starting an empty list
rearrange = []
#adding the letters from word to this list
rearrange.append(word[i])
# getting rid of brackets, commas etc. NOT SURE HERE
"".join.rearrage
print rearrange
return rearrange
reverse("cat")``````

#2

You create the empty list inside of the loop. That means on every iteration you go back to an empty list.

#3

``````def reverse(x):
#make sure x is a string
word = str(x)
#counting backwards from the length by -1
for i in range(len(word)-1, 0, -1): #SHOULD  BE -1 NOT 0
# starting an empty list
rearrange = [] #MOVE THIS UNDER word = str(x) OR ELSE LOOP WILL EMPTY THE LIST EVERYTIME
#adding the letters from word to this list
rearrange.append(word[i])
# getting rid of brackets, commas etc. NOT SURE HERE
"".join.rearrage #REMOVE THIS (side note: check spelling and the right syntax is eg. "".join(lst))
print rearrange #DON'T NEED THIS
return rearrange #SHOULD BE "".join(rearrange) NOT JUST rearrange
reverse("cat") #IF YOU WANT TO PRINT: print reverse("cat")``````

#4

Thanks. Also, the steps in range need to be range(len(word), -1, -1) not range(len(word), 0, -1) or else it stops before adding the first letter on the end of rearrange.

#5

Right, forgot about that. `range(len(word)-1, -1, -1)` *

#6

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