Reverse (7/15)



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?


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.


It is.

Thanks for the quick response!


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.


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