0
What does "string index out of range" meaan?
Got this error while trying to iterate https://code.sololearn.com/cnVL393rmqWS/?ref=app
13 Answers
+ 1
GaME OvER
Oh I got it !!!!!
Said nothing, your loop is good. The problem is within the loop !
You're looping through the "words" arrays, but you're affecting something else in the "words" variable inside the loop, so "words" is not your array anymore !!!
+ 1
Share your code on Description section so people can see it and help you with it. In case you didn't know how to share your code, follow the instructions on this post:
https://www.sololearn.com/post/74857/?ref=app
+ 1
https://code.sololearn.com/cnVL393rmqWS/?ref=app
+ 1
~ swim ~ is right !
Though, as you're working with python, an easier way to iterate through a string is to simply use the for - in loop, just like this :
for el in arr :
#your code
It will take every string (named el) in your array (named arr) and will execute the body of your loop for each one of them
+ 1
GaME OvER
Yes of course it can be used doing a while loop, but then you have to be careful about the way you loop, as you need to keep track of indices.
In your example, as you use <= in your condition, 3 is a valid indice (as your array contains 3 elements), but that leads you to try accessing the 4th element of your array (as the first indice of an array is 0)
So the right way to do it in your case is to replace the <= with a <, so that you don't go past the end of the array !
0
What are the ways to remove that error..
0
Can you add your code ? I wonder if it's the way you're iterating that is causing some problems and it would be way easier to help you if I know exactly what is wrong
0
Well could it be done using while?ThewyShift
0
And isn't this pretty much the same..
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2435/
0
Thanks all
0
You just have to change the name of the variable you use within the loop