FAQ: Learn Python - Practice Makes Perfect - reverse

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

also, is your observation about it stopping correct?

out

maybe it was, and you just expressed this a bit weirdly:

when it was that 0 wasn’t added, and changing the condition to >= would indeed include that

Apologies for the late reply. work…
Thank you for your help.
When I run my code in Jupiter Notebook it works it work correctly.
I think the problem is that in code academy notebook somehow “Python!abc” got cached.

image
image

codecademy calls the function multiple times for validation, because you use print this shows in the output

the problem is that your function prints the reversed string, it doesn’t return the reversed string (which is a requirement of the lesson)

they are not identical, text[1] vs text[l]. I add admit the letter l (like Lima) and the number 1 (first) look similar, they are not. And cause different behavior in the code

Thanks for the reply! It looked exactly the same in Codecademy and in my text editor but the second I inputted the preformatted text in the comment, I saw the difference and deleted my comment right away haha. It didn’t make sense to me at all why text[1] would work but I should’ve realized that and initially used a different variable than a lowercase L. Again, thanks for replying!

1 Like

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

print reverse(“Hello World”)

I have no idea what does it mean at all .

Okay, so what is your question? You could start by breaking the problem down in small steps. Or use a tool like this one to step through your code, that can also give a lot of clarification.

I do not understand how to do this and am very confused, Please help me! All I have this far is:
def reverse(text):

  • for char in text:*

Before writing code, think how you are going to approach this. How i am going to reverse this? Could i loop over the string in reverse and append each letter? Or could i loop regular and prepend? What code do i need for this?

Hi, I saw that the "worked, but failed coding section was closed so I am posting here.

I developed this code in the exercise, and accordingly to my print statements, it works perfectly. Though the platform doesn’t consider it. Why?

def reverse(text):
  text = str(text)
  print (text)
  new_text = ""
  for i in range(len(text)-1,-1,-1):
    new_text = new_text + text[i]
    
  print (new_text)

i think the error message is pretty helpful:

Your function fails on reverse(“Python!”). It returns “None” when it should return “!nohtyP”.

the keyword here is return, your function does indeed not return anything, resulting in None (the absence of a return value), you should return the reversed string

This is getting frustrating with Codecademy. I am doing the reverse exercise in loops. I have gotten my code perfectly correct and I have gone through is about a thousand times. This time; howerever, it is asking me to indent the entire block by one tab. If I don’t do this, it cuts off the first letter in the string and then reverses it. Why is this happening?!?!? I was going through the battleship exercise and it wasn’t working either, the responce I got was because there’s a bug.

Why does this code return “cba!nohtyp”?

The return statement should not be inside the loop. That’s the only issue we can see. Is that your complete code?