Reverse.help please


#1



It's obvious i'm doing something wrong. Can someone assist me with this problem. Thanks in advance guys.

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


Replace this line with your code. 
def reverse(text):
    t = []
    for i in range(len(text)):
        text.append(t)
        return t


#2

a function ends the moment a return keyword is reached, you might want to change the indention of the return so the whole for loop can run


#3

Thanks, is that all I have to do or I still need to modify it a bit ?


#4

there is more to be done. Currently you are appending t to text, not sure that is right, and you are not reversing anything


#5

Oopsie yeah you're right. Thanks for the call out, I will fix it :sweat:


#6

First, try unindenting

return t

so it's on the same indent as your for loop statement.
Now, the append() 's syntax is \

Targetlist.append(item[index])  #where index is optional

I'm assuming you want to put the reversed phrases in your new list, t. So, your Targetlist should be t. As for item, since it's in the loop, you are iterating all the letters in it. I suggest you append the text backwards to your new list t. You can do that like this:

1.Set your index variable equal to the index at the end of the list:

 index = len(text)-1

2.In append function, set the [index] with your index variable
3.Subtract index variable by 1
4.Lastly, in your return, join the list of strings into one string like so:

"".join(t) #If you don't do this, you'll get a list of stings eg. ["!","y", "e", "H"]

#7

def reverse(text):
t = []
chop = len(text)- 1
for i in range(len(text)):
text.append(t[chop])
chop -= 1
return "".join(t)

Still not working. You meant subtract by 1 after the .append right ?


#8

 t.append(text[chop]) #You did it the other way

The rest is good :slight_smile:


#9

Wait we haven't won the battle just yet. :weary: Now it's saying "Oops, try again. Does your reverse function take exactly one argument (a string)? Your code threw a "'str' object is not callable" error."


#10

Can you show me what you have now with proper indents?


#11

Sure

def reverse(text):
t = []
chop = len(text) - 1
for char in range(len(text)):
t.append(text(chop))
chop -= 1
return "".join(t)


#13

Sorry I don't know how to take a screenshot but yupp except the return line is right underneath "chop -= 1" and align with "for"


#14

Lmaooooo wow All I had to do was change the parentheses on "t.append(text[chop])"


#15

Thanks for the help, I appreciate it. I really do :slight_smile:


#16

Haha, stuff like that happend xD I'm glad you solved it.