Reverse (with a while loop)


#1



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


Link to specific exercise above ^ ^ ^ ^ ^ ^ ^
The error message says that it returns "!" instead of "!nohtyP". I have no idea what I have done wrong.


I've needed help on all the exercises in this lesson so far, and this one is no exception. How can I get it to return the reversed string? P.S. This question is somewhat unrelated, but can a "return" statement in a "while loop" break it or cause it to exit the loop?


def reverse(text):
    characters = len(text)
    while characters >= 0:
        reversed_text = "" + text[characters-1]
        characters -= 1
    print reversed_text 
    return reversed_text


#2

Right now your program is not able to add/concatenate all character from text (right to left characters).
so you get last character !.

You need to define an empty string ..

reversed_text = "" at starting of your function and then use it to catch all characters in it.

use this skeleton..

empty_string = empty_string + character

One more thing to note..

Your while expression
characters >= 0, as you're subtracting 1 from character-index,It will also catch character at at index -1 which is ! again, so characters > 0 would be enough ?

This question is somewhat unrelated, but can a "return" statement in a "while loop" break it or cause it to exit the loop?

return statement only makes sense inside a function.
as while-clause is inside the function so It has privilege to use return statement.
Yes it acts as break, when execution hits the return statement, It breaks out from function (as while loop is inside it so we can say "It break out from loop) but unlike break it can return a value.

Note:
In the program we're not using return inside the while loop.
It's a separate blocks of code as we can see return statement and while -block have same indentation level.


#3

Okay, so this is how my code turned out. I got it to work but it took some trial and error.
I defined an empty string at the start of the function as suggested:
reversed_text = ""
Then, I tried to run it, but it didn't work.

So, I decided to make
characters > 0 like this:

def reverse(text):
    reversed_text = ""
    characters = len(text) 
    while characters > 0:                             
        reversed_text = "" + text[characters-1]
        characters -= 1
    print reversed_text 
    return reversed_text

and it it returned "P" which was very confusing. (Maybe an explanation why would help :slight_smile: )
So, I decided to instead do this:

def reverse(text):
    reversed_text = ""
    characters = len(text) -1
    while characters >= 0:
        reversed_text = reversed_text + text[characters]
        characters -= 1
    print reversed_text 
    return reversed_text

And it worked! But, why did it work? Because when I reversed the position of the -1 (As it's position was in my original code).like this:

def reverse(text):
    reversed_text = ""
    characters = len(text) 
    while characters >= 0:                             
        reversed_text = reversed_text + text[characters-1]
        characters -= 1
    print reversed_text 
    return reversed_text

It returned "!nohtyP!" instead of "!nohtyP" and I don't understand why.

Thank you for helping me get my code to work.


#4

The first solution did not worked, why ?
reason is this line...

reversed_text = "" + text[characters-1]

You can see it's not able to add / update the string by adding characters in it?
Cus we're adding the character with '' and saving it in reversed_text.
At each iteration it gets a new character so at last it gets ! and thus it gets saved in it and we have that as output.


The second program.

The problem mentioned in first code is removed by this line..

reversed_text = reversed_text + text[characters]

Now you're adding every characters to reversed_text and we're able to catch all the values in it as reversed string.


Third program

It returned "!nohtyP!" instead of "!nohtyP" and I don't understand why.

As I mentioned in my first post.

characters >= 0, as you're subtracting 1 from character-index,It will also catch character at at index -1 too which is ! again, so we get extra ! in our reversed string.?


try this tool to visualize your first/second/third code, You will know what actually happening at each step and why the first and third program did not worked out!

if you still find yourself in confusion, point them out and I will explain!


#5

Thank you so much. :slight_smile:


#6

Hello!

I finally solved the problem with the code below. I see that many of you solve this with an while loop. What are the benefits of using a while loop over my solution? Thanks!

def reverse(text):
    
    length = len(text)
    result = []
    
    for letter in range(length):
        result.append(text[length-1])
        length -= 1
     
    return "".join(result)

#8

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