1

I recently started learning Python from a MOOC on Coursera. I'm trying to write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.

I have written the code which is giving me the desired output but it's also giving me an error

"IndexError: string index out of range"

index = 0
fruit = "potato"
while index <= len(fruit) :
    index = index - 1
    letter = fruit[index] 
    print(letter)
 Traceback (most recent call last):
      File "strings_01.py", line 8, in <module>
        letter = fruit[index]
    IndexError: string index out of range
1
  • Thanks for your reply, I'm sorry if the above code looks stupid, I've just recently started to learn. I tried your recommendation and it doesn't work. It gives me the same error. Commented Jun 4, 2019 at 11:00

5 Answers 5

1

Try using a different while loop condition:

index = 0
fruit = "potato"
while abs(index) < len(fruit):
    index = index - 1
    letter = fruit[index] 
    print(letter)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this works perfectly fine without any errors. Appreciate your help.
Honestly, I prefer my answer since it doesn't need the abs() function call at every iteration.
@PrashantGonga Happy to help :P
@alec_djinn Well, okay, but OP preferred mine, also self-advertising isn't good
1

This will work. Of course, it's just for the sake of learning, there are better ways to do that in Python.

fruit = "potato"
index = len(fruit) -1 #Python indexes starts from 0!
while index >= 0 :
    letter = fruit[index]
    print(letter)
    index -= 1 #decrease at the END of the loop!

Output:

o
t
a
t
o
p

Comments

0
fruit = "potato"
index = len(fruit)
while index > 0 :
    index = index - 1
    letter = fruit[index] 
    print(letter)

1 Comment

This code runs without any error but the output doesn't seem to be correct. Here is the output I've got. o t a t o p o There is an extra "o" added to the output at the end
0

This is what you are looking for

index = 0
fruit = "potato"
while index > -(len(fruit)) :
    index = index - 1
    letter = fruit[index]
    print(letter)

Comments

0

Try this:

>>> fruit = "potato"
>>> fruit = fruit[::-1]
>>> fruit
'otatop'
>>> for letter in fruit:
...     print(letter)
...
o
t
a
t
o
p

Alternatively with while loop:

>>> fruit = "potato"
>>> fruit = fruit[::-1]
>>> fruit
'otatop'
>>>  index = 0
>>> while index < len(fruit):
...     print(fruit[index])
...     index+=1
...
o
t
a
t
o
p

7 Comments

The OP wants to use a while loop to do that.
@alec_djinn still what is the issue if I use for. But I will respect your comment and will edit answer.
Thanks for your help, I need to write this code only using while loop
@PrashantGonga I have added the answer with while loop.
@AmazingThingsAroundYou The issue is that the OP asked a solution involving a while-loop and you are answering with a for-loop. So, technically, it's a wrong answer. The code will work, but the answer doesn't fit the specs of the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.