FAQ: Learn Python - Practice Makes Perfect - reverse

faq

#1

This community-built FAQ covers the “reverse” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise reverse:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!


#2

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

#4

turnt =
def reverse(text):
print text
for let in str(text):
print let + " let"
turnt.insert(0, let)
print turnt
return turnt

Returns the following,

abc
a let
[‘a’]
b let
[‘b’, ‘a’]
c let
[‘c’, ‘b’, ‘a’]
Python!
P let
[‘P’, ‘c’, ‘b’, ‘a’]
y let
[‘y’, ‘P’, ‘c’, ‘b’, ‘a’]
t let
[‘t’, ‘y’, ‘P’, ‘c’, ‘b’, ‘a’]
h let
[‘h’, ‘t’, ‘y’, ‘P’, ‘c’, ‘b’, ‘a’]
o let
[‘o’, ‘h’, ‘t’, ‘y’, ‘P’, ‘c’, ‘b’, ‘a’]
n let
[‘n’, ‘o’, ‘h’, ‘t’, ‘y’, ‘P’, ‘c’, ‘b’, ‘a’]
! let
[’!’, ‘n’, ‘o’, ‘h’, ‘t’, ‘y’, ‘P’, ‘c’, ‘b’, ‘a’]

how do I stop this from happening?


#5

if you call the call the function multiple times (please try this), you will see that the list accumulates data from all the function calls, so the list doesn’t “reset”. How could we fix this?


#6

Well what I tried was to call break after the function so that it would stop the process and then start over a new. But what it did was went through and gave me C, B, A and completely stopped and didn’t do anything on the next Python argument. Then I try to .clear() and that didnt work. I’m not sure if that’s a Python three thing and I was looking at the wrong Python version for a solution. But that didn’t work so I guess I could try to maybe do pass but I think it will do the same thing as break. I could do it in a certain way in which I could say if we find the a or B or C delete them but that’s not the spirit of what they’re trying to get me to do. And it doesn’t look like it would work for all arguments plus you have a problem with the first argument and maybe I could do it in a on the first round accept abc and on the second round not accept. But then I’m not actually finding the solution to my problem. I could maybe enumerate the list and say however many are enumerated delete the same before you add a second list. Just seems like there should be a way easier solution.


#7

there should be, and there is.

The list now accumulates data because you define it once when the program/script start running, so as long as the program runs, the list just gets more and more data

so that doesn’t seem to be the right place to define the list


#8

turnt[0:-1] =
no good here


#9

yes
I seeeeee! For some reason I did not catch that but now that I’m thinking about it I’m remembering5 watching videos about objects and how their variables are only accessible within the object. Data were not there yet I just seen some videos on it and it makes sense to me now is wondering why wouldn’t this reset but it’s not inside the functions would not.


#10

Thank you very much I totally didn’t see that I started making the function I realized that I needed a variable and it is part of the top because it was easier for now I see the importance of putting the variable in the function instead of where it is most convenient on the page. My bad


#11

putting variable at the top (or nearly) also has its use cases, but not in this case

do you understand now?


#12

Yeah definitely catch it in the function I need to reuse it and I needed to clear out every single time the once my variable is outside of the function it’s a variable that has the ability to be added to from various functions. But if I needed just for the function and not for some more global use it’s very problematic to put it outside of the function since the list has no global purpose just taking it harder on myself


#13

4 posts were split to a new topic: Works, but CC fails it


#14

why have they -1 away from the len(text) in the solution, would that then take one of the letters out from the word. I thought len() gave you the total number of characters in a string so wouldn’t this remove a character?

l = len(text) - 1


#15

Not sure what the given solution is but what it sounds like is a reverse range. In that case, the starting value is the highest index, not the lowest.

range(0, len(x))    # lowest to highest

range(len(x), 0)    # highest to lowest (almost)

The latter requires a negative stride, so it would be,

range(len(x), 0, -1)

however that too is not correct. There is no element at x[len(x)], and the stopping value is 1 higher than the actual last index so,

range(len(x) - 1, -1, -1)

#16

What’s stopping you from finding out if it’s one too little?
Do you know what everything in the code does? Evaluate it, observe what happens.
It’s not a black box, you can look at whatever you want while it happens, you can modify it, you can print out information, you can observe the result.

What would happen if it’s the right amount? Run it, and test whether that happens.
What would happen if it’s one too little? Run it, and test whether that happens.


#17

I was wondering the same thing and followed your advice by looking at the code.
My thinking is that the way the code is written with the use of a while loop… you need to start on the last character that has 10 for index. Therefore, length of (text) returns 11. But we start at 10.
Hopefully that’s the right thinking :smiley:


#18

Still don’t quite understand the concept of taking 1 from the ‘len(text)’ when using the ‘range’ tool to iterate through all of the letters in the word, like so:

Surely it would start at the 4th letter for a 5 letter word, and the 5th letter would be missed out?


#19

The last location and the length of a list are different things. If you know one then you can figure out the other. You can’t use the length to say which the last location is since that’s not the same thing.

If I have a container that is 75% full, then how much space remains in it? It’s not 75%, but you can figure out that it is 25%

All you really need to do here is consider what range’s behaviour is, what you want it to do, what information you have and how you can use that to feed appropriate information to range. Nobody needs to explain to you why 1 needs to be subtracted!

So, what does the first argument mean to range?
Yeah, you have to look that up, there’s no way around it.
And not just for range, but for absolutely everything.
There’s no guessing required or allowed when everything has exact definitions.