7. Reverse. Please help. Please


#1

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 ^_______^


#2

@textslayer23704,

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

    while length_text >= 0:
        reverse_text.append(text[length_text])
        length_text -= 1
    print reverse_text
    print "-".join(reverse_text)
    print "".join(reverse_text)
    
reverse("abcd") 
#should return "dcba".

#3

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.


#4

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.


#5

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.


#6

@ajaxsolver02199, @blakec90,
With google search a couple of interesting Posts
== discussions / opinions ==
python string concatenation best practice site:stackoverflow.com
like
http://stackoverflow.com/questions/10043636/any-reason-not-to-use-to-concatenate-two-strings


#7

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)


#8

@textslayer23704,
With "-" i wanted to demonstrate what you can do with the join() Method
google search
== discussions / opinions ==
Python list join() site:stackoverflow.com
http://stackoverflow.com/questions/493819/python-join-why-is-it-string-joinlist-instead-of-list-joinstring


#9

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


#10

this is cool. never thought of that.

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


#11

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!


#14

@skidfoma

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


#15

@ajaxsolver02199
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?


#16

thz for sharing........


#17

really nice and simple ! good stuff... kudos


#18

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
else:
str(new_text)
return new_text


#19

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


#21

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


#22

OHHH I SEE

Thank you!


#23

Hey,

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

Thanks!