Python IndexError

Not sure what’s going on here.
First, why I’m getting an error in a line that doesn’t exist?
And second, Why I’m getting that error?
The first while loop works fine. I’m getting a list (distList) with 7 items, so, count1 is 7.

The line number on the error message will usually return the line number that is 1 greater than where the line is (though it can be more if your code uses definitions to execute). In this case the error is on line 20.

I think this error is happening because you’re asking Python to find the item at index zero, when such a thing doesn’t exist, as repDist is an empty list. It would be like asking me what the first thing on my shopping list for next weekend is today - I have the paper set up to write it, but it’s blank, so I can’t tell you what that will be yet. Actually, that will be coffee… it’s always coffee. Ask me for the second item though and that’s a complete unknown.

Kidding aside, try changing line 19 to repDist = [ distList [0] ] and skip line 20 entirely, that should give you your first value from the distList as the first value in your repDist when you define it.

1 Like

Thanks again Jacob. It worked!!!
I liked your example, tho

1 Like