Reverse


#1


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


why the below code error? please any advice for me..!!!


def reverse(text):
    
    reverse_text=""
    l=len(text)
    j=0
    while l<0:
        reverse_text[j]=text[l-1]
        l=l-1
        j=j+1
        
    print reverse_text
    
    return


#2

a loop runs while a condition is true:

while l<0:

this condition is false the first time it should run. l contains the length of text, 0 is not greater then l. maybe use > instead?

then this line:

reverse_text[j]=text[l-1]

you can't assign values on index into a string, you could make reverse_text a list? and then join the list into a string at the end?

then return reverse_text at the end, don't print it (or do both print and return, that is also fine)

if you need more help, post an updated version of your code, but i recommend you to try first, with the insight i (hopefully) provided you


#3

Thanks for your reply.. I will more try and then I will update newly..


#4

very good, if you can't figure it out, post an updated version of your code and i will help you further


#5

So here's what I've tried.

def reverse(text):
    rev_text = []
    final = ""
    for char in text:
        rev_text.append(char)
    for letter in rev_text:
        final += rev_text.pop()
    return final

Now the code works, but it only returns half of the word before it stops. For example, 'test' returns 'ts' and 'trialrun' returns 'nurl'. Is there something about .pop() that I should know about? What is my error here?


#6

look at this code:

def reverse(text):
    rev_text = []
    final = ""
    for char in text:
        rev_text.append(char)
    for letter in rev_text:
        print final, rev_text
        final += rev_text.pop()
    return final
print reverse("Python!")

it is your code with some print statements + function call. rev_text gets smaller because of pop, so at certain point the loop has ended, because of the all items in the list which have popped.

from the documentation:

Remove the item at the given position in the list, and return it. If no index
is specified, a.pop() removes and returns the last item in the list. (The
square brackets around the i in the method signature denote that the parameter
is optional, not that you should type square brackets at that position. You
will see this notation frequently in the Python Library Reference.)

so pop() pops (removes) items from the list. Hm.. do you think with this new insight you can get further or do you need more help?


#7

So I guess I still need a bit more guidance. I understand that the .pop() function removes items from a list, but shouldn't it continue to remove items until the list is empty? rev_text still contains additional items after the function stops, and I'm not sure why that is happening.


#8

lets make one more modification to your code:

def reverse(text):
    rev_text = []
    final = ""
    for char in text:
        rev_text.append(char)
    for letter in rev_text:
        print final,rev_text, letter
        final += rev_text.pop()
    return final
print reverse("Python!")

now 3 things are printed, final, rev_text and letter, as you now hopefully can see, the for loop runs out of letters, given you are removing them from the list

the easiest solution would be to make a copy of rev_text (if you are lazy, use the built in list() function, loop over this copy of rev_text and pop the items from rev_text, but given you are looping over a copy of rev_text, the loop shouldn't end prematurely.


#9

Ok, so I think I understand what you're saying. I modified my code like so:
def reverse(text):
rev_text = []
final = ""
for char in text:
rev_text.append(char)
dummy = list(rev_text)
for letter in dummy:
final += rev_text.pop()
return final

and now it runs great. So if I'm to understand what was happening, let's say I have a list that has 8 indices. [0,1,2,3,4,5,6,7]. .pop() would spit out and remove 7 from the list, leaving 7 indices [0,1,2,3,4,5,6]. But the for loop essentially thinks that it's done two iterations, because it did one and the list is now one shorter. And that continues until it runs out halfway through the word. Is that about right?


#10

yea, that is what is happening indeed :slight_smile:


#11

Awesome, thank you so much for your help. :slight_smile:


#12

2 posts were split to a new topic: Reverse


#13

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