I've been reading other answers for "Index out of range" questions, and from what I understand, you can't try to assign a value to a list index that doesn't exist. My first code was:
spam =[]
for i in range(5):
spam[i]=i
print (spam)
After reading some of the other answers, I realized that, when I used just the range
function, i
was starting at 0 and couldn't be assigned to the list. So I created the next little bit of code:
spam =[]
for i in range(5):
i += 1
spam[i]=i
print (spam)
And I'm still getting the same error. I know that i
now gains the value of 1 after the i += 1
portion of the code, so I believe it should be assigning list item 1 as number 1 in the empty spam list, but it's not working that way. Why is the index not being created?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…