7. Reverse. Please help. Please


def reverse(text):
reverse_text = []

while len(text) >= 0:
    length_text = len(text)
    reverse_text.append(text[length_text - 1])
    length_text -= 1

The code keeps showing this error --> Your code looks a bit off--it threw a "" error. Check the Hint if you need help!

This is the goal of this exercise:
Define a function called reverse that takes a string textand returns that string in reverse.
For example: reverse("abcd") should return "dcba".

please help, I'm about to die of frustration ^_______^



def reverse(text):
    reverse_text = []
    length_text = len(text)-1

    while length_text >= 0:
        length_text -= 1
    print reverse_text
    print "-".join(reverse_text)
    print "".join(reverse_text)
#should return "dcba".


I see people using lists and whatnot for the function. Is there anything wrong with what I did below? It's very simple and satisfied the lesson.


That's a neat idea. What was your thought process when creating it?
Why did you add letter = char if you used char later (a place holder to keep in mind what char represents?). I tried your code without letter = char, and it still worked.

Thanks for sharing.


Nice pickup ajaxsolver02199, I think it's leftover code when I was trying (and failing) different things.

I assumed that strings would join together in the order they are typed, and my code worked first run. If you reverse Line 5 order, the function gives an error, so it seems to be legit.


@ajaxsolver02199, @blakec90,
With google search a couple of interesting Posts
== discussions / opinions ==
python string concatenation best practice site:stackoverflow.com


Thank you so much! But I was wondering why this is used:
print reverse_text
print "-".join(reverse_text)
print "".join(reverse_text)

instead of just:
print "".join(reverse_text)


With "-" i wanted to demonstrate what you can do with the join() Method
google search
== discussions / opinions ==
Python list join() site:stackoverflow.com


ahhh I see, that makes sense. Thank you so much!


this is cool. never thought of that.

btw, what's that 'letter = char' for?


Can someone explain to me why this works? I'm seeing a lot of complex and difficult code out there for this lesson, but this is extremely simple. How does python know what to do without and for functions or if/else functions???
Thank you!



oops, codecademy bugged and posted my answer 4 times :neutral_face:


Here in:

for char in text:

How does char pick up the last letter in the string and so on? Is it primarily supposed to do so, return the last letter first in a string?


thz for sharing........


really nice and simple ! good stuff... kudos


Here's another solution to this...
CA accepts it, and it's simple and to the point!

def reverse(text):
new_text = ''
text_len = len(text)
while text_len > 0:
new_text += (text[text_len - 1])
text_len -= 1
return new_text


This works but I don't understand why. What does the rev_text variable do and how does it reverse?


the rev_text stores the letter while the for loop loops through every letter, and push that letter to the end => char+rev_text, see the new char is located before rev_text
if you have 'abcd', then 'a' is saved and 'b' is insert before 'a' in the second round, thats how it works.
the order would be a, ba, cba, dcba
and letter = rev_text is not needed



Thank you!



Can you explain line 4: What does it do/mean? What is 'letter' here?