FAQ: Learn Python - Practice Makes Perfect - reverse

turnt[0:-1] =
no good here

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.

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

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

do you understand now?

1 Like

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

1 Like

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

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

1 Like

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)

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.

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:

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?

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.

2 Likes

I don’t understand how the solution prints the letters reversed?

We have len(text) - 1 which equals a number, not a letter. For example if we input “star” where text is that would amoun to 3 (4-1). So the solution should print out numbers, not letters.

Please someone help me understand .

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

l is the index, text[l] is the letter. Starting at the highest index and working down to zero, access the last letter and append it to the return string. This results in a reversed string.

As shown in staffsar’s response, in the solution, it first converted

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

In my solution, I tried to use

while len(text) >= 1:
word = word + text[len(text) - 1]
len(text) = len(text) - 1

but I get “SyntaxError: can’t assign to function call”

My question is: why doesn’t len(text) = len(text) - 1 work?

Pretty much says it all. If we wish to add to length, then we must add to the list. The len() function evaluates the length of list and returns a value. Something that returns a value cannot be assigned to, but assigned to something.

The string “Hello world” is 11 characters long (including the space). Since our condition is while l >= 0, for the loop to end at l = 0, we need (10 to 0) eleven subtractions, with initial value l =10.

Help Please!
What’s wrong with my “while”?
My idea was to create a list using “while”
So I start with list =
and add numbers
so it’s list=[5,4,3,2,1,0]
But my “while” stops after list=[5]

def reverse(text):
  list_=[]
  x=len(text)-1
  while x > 0:
    list_.append(x)
    x-=1   
  else:
    for n in list_:
      print text[n],
  
  return

Remove all other code than what you’re specifically looking at so that there’s less going on.

A while-loop continues for as long as the condition is met. What variables are used in the condition, what values do they have? That’s something you can print out and look at.

To post code on a forum you’ll need to tell the forum not to apply regular text formatting rules, to leave it alone. There are a couple of buttons in the editor, one of them is for that.

It looks a bit as if though you’ve got while-else in your code. What does that do? I expect you don’t know, but you should be aware of that you don’t know that - because you can’t possibly leverage it if you don’t know what it does. You need to be super deliberate about everything you write.

I would expect that as long as “while” condition is met , so x>0 (should be x>=0) “while” will be repeating the sequence and move to else when x<0