Reverse - thank you in advance


#1




N/A


Could really do with a nudge in the right direction. I am finding all of these particularly challenging however, I just go mind blank when it comes to accessing a list back to front. Its like my mind just say NO and shuts down.
I can .append() the letters individually to my list but then my mind leaves me at the door, locks it and throws away the key.

Would appreciate some light being shined and thank you in advance.
Hope i have posted this correctly :S sticking to the template! Also I am only 1 week into learning Python so I am not being too harsh on myself...well trying not to


 
def reverse(text):
    new_word = []
    i = len(text)
    for l in text:
        new_word.append(l)
    print new_word
           
           
reverse("python")


#2

maybe you can use range?

this will give you indexes you can work with, but with range you can also specify start (in your case end of the string), stop (beginning of the string) and step (-1 for from high to low)

Is this a good enough, or do you need more?


#4

Edit: I have changed the print new_word to return new_word and this has worked.
Any explanations on this as to why it worked?

Thanks Stetim94 for all your assistance, appreciate your help

OK So I think I have cracked it in that my algorithm (if I may call it that?) works when I input the function with my own argument. However, I get the error message:
Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP" - however, when I type in reverse("Python!") it reverses it in my console perfectly?

Any ideas?

def reverse(text):
    new_word = []
    i = len(text)
    text = str(text)
    for l in range(len(text)-1,-1,-1):
        new_word.append(text[l])
    new_word = "".join(new_word)
    print new_word
    ^^ changed this to return new_word and it has worked...any ideas why?
           
           
reverse("Python!")

#5

i would use .join() to get from list to string, it is the only effective way.

"".join(list)

the character between the quotation marks is what is used to join the items in the list together

seems you figured this out while i was typing, you need to return the reversed string


#6

Haha we keep crossing each other! Thank you for your help.

Please could you explain why I needed to return it?


#7

because the exercise wants you to return the reversed string


#8

That's great - thanks for your help!


#9