I have tried to create a list of whole numbers under 1000 (aka, 0 to 999), then delete the numbers that don't comply with a certain rule [ex. must be divisible by 7 or 3]. After, I have find the sum of all these numbers. Below is the code I wrote to do so.
number_list = []
for x in range(1000):
number_list.append(x)
index = 0
element = number_list[index]
max = 1000
deleted = 0
while index < max - deleted:
element = number_list[index]
if element % 7 != 0 or element % 3 != 0:
number_list.remove(element)
deleted = deleted + 1
index = index + 1
print(sum(number_list))
This runs without any issues, but it does not return the correct sum. The correct sum is 214216, but this gives me 261832 instead, leading me to believe that more than 40 elements that should have been deleted, have not been deleted.
How can I fix this problem?