range gives you an iterable object:
>>> range(10,20 , 2)
range(10, 20, 2)
>>> list(range(10,20 , 2))
[10, 12, 14, 16, 18]
The values in it are fully decided as soon as the call returns, and aren't re-evaluated each time around the loop. Your step only goes up to 337 because you are incrementing it once for each element in the object range(0, 1000, 3), which has 334 items, not 1000:
>>> len(range(0,1000,3))
334
To get something that works like range but advances the step, you would need to write your own generator:
def advancing_range(start, stop, step):
''' Like range(start, stop, step) except that step is incremented
between each value
'''
while start < stop:
yield start
start += step
step += 1
You can then do for i in advancing_range(0, 1000, 3): and it will work as you intend.
But this is a very strange thing to want to do. Judging by your variable names, I would guess you're coding the locker problem, which says:
A new high school has just been completed. There are 1,000 lockers
in the school and they have been numbered from 1 through 1,000.
During recess (remember this is a fictional problem), the students
decide to try an experiment. When recess is over each student will
walk into the school one at a time. The first student will open all
of the locker doors. The second student will close all of the locker
doors with even numbers. The third student will change all of the
locker doors that are multiples of 3 (change means closing lockers
that are open, and opening lockers that are closed.) The fourth
student will change the position of all locker doors numbered with
multiples of four and so on. After 1,000 students have entered the
school, which locker doors will be open, and why?
But the advancing range logic says something more like "the first student opens the first locker, then the second opens the second locker after that, then the third student opens the third locker after that ...". You want to affect multiple lockers each time, but further spaced out. Essentially, you want to copy and paste your first two loops another 998 times with a one higher step each time. Of course, you can do better than copy and paste, and this seems like you want two nested loops, where the outer one advances the step that the inner one uses. That would look like this:
for step in range(1, len(lockers)):
for i in range(step, len(lockers), step):
Simplifying your other logic by using booleans instead of 1 and 0, the whole program looks like this:
lockers = [True] * 1000
for step in range(1, len(lockers)):
for i in range(step, len(lockers), step):
lockers[i] = not lockers[i]
print(sum(lockers))
It prints that the number of open lockers is 969.
range()andenumerate()usage is considered more pythonic, but it really depends on your individual use case. In this case it's not real clear what the end intension is. What is your end goal?range(0, len(lockers), step)is only called once, with the initial values.