0

What does "string index out of range" meaan?

Got this error while trying to iterate https://code.sololearn.com/cnVL393rmqWS/?ref=app

17th May 2019, 5:23 PM
Ajay Nair
Ajay Nair - avatar
13 Antworten
+ 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 !!!
17th May 2019, 5:53 PM
ThewyShift
ThewyShift - avatar
+ 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
17th May 2019, 5:27 PM
Ipang
+ 1
https://code.sololearn.com/cnVL393rmqWS/?ref=app
17th May 2019, 5:35 PM
Ajay Nair
Ajay Nair - avatar
+ 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
17th May 2019, 5:42 PM
ThewyShift
ThewyShift - avatar
+ 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 !
17th May 2019, 5:49 PM
ThewyShift
ThewyShift - avatar
0
What are the ways to remove that error..
17th May 2019, 5:26 PM
Ajay Nair
Ajay Nair - avatar
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
17th May 2019, 5:29 PM
ThewyShift
ThewyShift - avatar
0
Well could it be done using while?ThewyShift
17th May 2019, 5:44 PM
Ajay Nair
Ajay Nair - avatar
17th May 2019, 5:46 PM
Ajay Nair
Ajay Nair - avatar
0
Thanks all
17th May 2019, 5:51 PM
Ajay Nair
Ajay Nair - avatar
0
You just have to change the name of the variable you use within the loop
17th May 2019, 5:53 PM
ThewyShift
ThewyShift - avatar