r/learnpython • u/Ayrton110 • Sep 24 '26
Getting the intended output, but still getting "out of range" errors
Hi guys, I hope ya'll are good, thank you. I am struggling with this problem for weeks now. I get the answer, but still get "out of range". I know with "banana" this would work because of a technicality. Is there anyway for this work without the error?
fruit = "apple"
index = - 1
while index < len(fruit):
letter = fruit[index]
print (letter)
index = index - 1Any input or advice is appreciated. Thank you for taking the time to read my post here.
Edit: Sorry, intended answer is "elppa"
I get this
e
l
p
p
a
IndexError: string index out of range
0
Upvotes
15
u/tinytimn Sep 24 '26
Well what is the length of fruit. It's a positive number right?
And what is your index value. Which way is it traveling.
5
u/good-mcrn-ing Sep 24 '26
What is the intended output?
3
u/Rubix321 Sep 24 '26
You haven't told us what "the answer" is supposed to be.
Do you know what fruit[-6] does?
3
u/D3str0yTh1ngs Sep 24 '26 edited Sep 24 '26
Your
indexstarts at-1and becomes smaller in each cycle of the loop.index < len(fruit)is thrus always true and the loop doesn't end, except for the fact thatfruit[-6]throws anIndexError(because "apple" is 5 letters and so there is no 6th letter from end.) and crashes your program.Do something like this instead:
2
u/Jazzlike_Platypus430 Sep 24 '26 edited Sep 24 '26
It doesn't work with "banana."
If your aim is to print "elppa", try to get the string's indices and reverse it.
On the lower level, like your attempt, do it properly:
7
u/No_Statistician_6654 Sep 24 '26
Have you tried running this with a debugger, and checking the value of index each time it is assigned and used?
That will help you understand where the out of range is coming from in the process.
6
u/This_Growth2898 Sep 24 '26
You really need to learn how to work with a debugger.
You get all the intended output, and then you get an error because the loop doesn't stop after you get your output. You start with
index = -1and keep doingindex = index - 1whileindex < len(fruit); butindexis negative and will always be smaller than a non-negativelen(fruit). You should change thewhilecondition.Also,
index -= 1is a short way to writeindex = index - 1. Andprint('\n'.join(fruit[::-1]))works, too.
1
u/Ayrton110 Sep 24 '26
Hi guys, I appreciate the assistance, the effort each of you have made to help me, and the fact that ya'll respond this promptly, but there are so many people I'm getting a little bit confused. If you have a clear idea of this, could you please just comment here. Some tell me to debug, others say change the sign, neither of these appear to be working.
1
u/HummingHamster Sep 24 '26
Even with banana you should be getting the same error. Did you try?
The logic of this is wrong. len(fruit) is 5. Your index starts from -1 and keeps reducing each loop and thus index will always be < len(fruit).
2
u/ChillBlock Sep 24 '26
you need a condition to stop the loop after the intended output
1
u/VShadowOfLightV Sep 24 '26
Index (-1) will always be less than len(fruit) (5). So it’s going to just keep going and going.
1
u/Educational_Virus672 Sep 24 '26
the thing your checking smt is low in a while loop that subtracts itself here it is not a good idea
what you checked was -1 -2 -3 -4 -5 -6 where -6 is out of word it is 5 letters long .even if you remove the error the numbers go on forever for this kinda code
try using for loop with a reversed variable
here [::-1] says [start:end: in negative order]
now just looping though each is easy
but you can make it better "looking" by removing [i] and changing it to just fruit
also the i is variable hence it is changeable to any word/letter
1
u/theWyzzerd Sep 24 '26
Indexes don’t wrap. You need to stop the loop before you read an index that doesn’t exist. Hint: check your
whilecondition.1
u/Kurt_C0841N Sep 24 '26
Try using Thonny :) It's good for showing you what happens each step of the way
1
u/Rezrex91 Sep 24 '26 edited Sep 24 '26
You don't start with -1 as index to make it go backwards (that's done by the
index = index -1line) and you have the wrong loop condition too. This would be the correct one:You only get output because Python treats negative indexes to strings as indexing backwards from the end, but if you start with an index at -1 and always increment it into the negative (so -2, -3, -4, ...) you'll never get to the
len(fruit)condition because that's a positive number, that's why you get index out of bounds error, because the loop tries to run forever but you can't go back further than the start of the string (sofruit[-5]is the end, thenfruit[-6]tries to read from before your string which is not allowed.)Another way if you must have negative indexing:
1
u/argh1989 Sep 25 '26
Because your index is negative it'll always be less that the length of the fruit which means the while condition will always be true. So it prints each letter that exists then tries to print the next one which doesn't.
1
u/Abject-Explorer-3637 Sep 26 '26
uh what