[Tutor] Why is my list index going out of range

Jim Mooney cybervigilante at gmail.com
Tue Apr 23 03:47:41 CEST 2013


Okay, what am I doing wrong here? I'm generating primes from a list
and getting "list index out of range," but since the for loops
controls the indexes I don't see how that's happening. I thought it
was the list append, but I commented that out and put in a print
statement, and I still got the line 5 error:

primeList = [1]
numList = list(range(2,101))
for e in numList:
  for f in primeList:
    if numList[e] % primeList[f] != 0: #list index out of range
      primeList.append(numList[e])

print(primeList)

--
Jim Mooney

The Real Reason Things Keep Going Wrong:

At the beginning of time, God set a Magic Top Spinning... and spinning...
and spinning... and spinning... and spinning... and spinning... and
spinning...

After a few hundred million years God got bored and gave the top a good
kick; so it went rebounding away, flinging off planets and stars and
dinosaurs, and later, people who wrote about dinosaurs.

Then God realized that although Order and Regularity are virtuous, if you
want interesting stories, now and then you have to give things a good Kick!

This explains a lot.


More information about the Tutor mailing list