Reverse (7/15)


#1

Hello.

I’ve yet to run into a wall during the entire Python course like I have with this particular exercise. After spending far too long on it, I allowed Codecademy to provide its own solution to the problem (listed below).

def reverse(text):
    word = ""
    l = len(text) - 1
    while l >= 0:
        word = word + text[l]
        l -= 1
    return word

I would like to understand the given solution better.

For example, in this line:

    l = len(text) - 1

What’s going on with the " - 1 " at the end of the line? Why is this necessary?


#2

len(text) is an out of range index due to zero-indexing.

Is this the Get Code solution? If so, somebody needs to be reminded that we do not use l as a variable since it looks like a 1. We don’t want to encourage poor practices.


#3

It is.

Thanks for the quick response!


#5

I hope you will stick with the lesson and work out alternate solutions. This solution can be refactored with a little thought. Don’t rob yourself of the rewards for engagement.


#6

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