So, I have 8 randomly generated numbers, all referenced with ct[i]. I want to add a number (ct[i]) with the one referenced by ct[i+1]. However, this produces a list index out of range error. What’s wrong?
for i in range(totrange):
tot1 = ct[i] + ct[i+1]
totrange is usually 8, but I wanted to have a bit of flexibility.
You should probably use
range(len(ct)-1)to handle this issue, as for the lasti,i+1is a value which is greater than the last index ofct.examples:
or better use a
zip()based solution, no need of using indexes at all: