i wrote a program to evaluate what numbers in a certain range are divisble by only certain numbers (in the range 1 to 9). So far the code seems to work but i tested the steps it takes through at pythontutor http://www.pythontutor.com/visualize.html#mode=edit and something odd happened.
In the second loop, the code doesnt always check all values (k) for its divisibility but sometimes leaves out the last value (k).
Better to give you an example:
Liste = []
for i in range (1500, 1700):
if i%5 == 0 and i%7 == 0:
Liste.append(i)
print (Liste)
Teiler = [2, 3, 4, 6, 8, 9]
for k in Liste:
z = 0
for w in range (len(Teiler)):
if k%Teiler[z] == 0:
Liste.remove(k)
z += 1
break
else:
z += 1
print (Liste)
Here the output is as follows:
[1505, 1540, 1575, 1610, 1645, 1680]
[1505, 1575, 1645]
And its the output that its supposed to be, as in the second list are only the numbers divisible by 5 and 7. Also on pythontutor the second for loop goes through all values (k).
But when i change the range of number like the following (line 2, change range from 1700 to 1800):
Liste = []
for i in range (1500, 1800):
if i%5 == 0 and i%7 == 0:
Liste.append(i)
print (Liste)
Teiler = [2, 3, 4, 6, 8, 9]
for k in Liste:
z = 0
for w in range (len(Teiler)):
if k%Teiler[z] == 0:
Liste.remove(k)
z += 1
break
else:
z += 1
print (Liste)
the output is as the following:
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785]
[1505, 1575, 1645, 1715, 1785]
If i run this code on pythontutor, the code stops at k = 1750 and doesnt check for k = 1785.
Why is it behaving that way? Is it a problem with the check on pythontutor? Or is there a problem with my code?
I want to understand why Python is doing what it does.
Thank you very much for your help. Iam sorry if this is a noob question or iam missing out something obvious.