0
import math

maks=18
tab = []
for i in range(2, maks + 1):
   tab.append(i)

end = int(math.sqrt(maks))
for j in range(2, end + 1):
   for c in range(len(tab)):
      if tab[c]%j ==0 and tab[c] != j:
         del tab[c]

Error:

if tab[c]%j ==0 and tab[c] != j:
IndexError: list index out of range

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • Deleting from the list you're iterating over is usually a recipe for disaster. – Fred Larson Jan 02 '18 at 17:06
  • 1
    Because you change the length of the list while you're iterating over it: `del tab[c]`. This also breaks the sieve; you should set the value to e.g. `None` then filter it afterwards. – jonrsharpe Jan 02 '18 at 17:06

0 Answers0