In python for loop, jump over values

time=0
gold=0
level=1
for time in range(100):
  gold+=level
  if gold>20*level:
    level+=1
    time+=10

with this program, gold is added until it reaches a critical amount, then it will take 20 seconds to upgrade the mine so that it produces more gold. would I like to skip these 20 (or 20 steps) in a loop? this works in C ++, I'm not sure how to do this in python.

+5
source share
3 answers

Do not do this in range(100). The loop fordoes not offer a way to skip this; timethe next value in the list will be set, regardless of what you change it to in the body of the loop. Use a loop instead while, for example

time = 0
while time < 100:
   gold += level
   if gold > 20 * level:
      level +=1
      time += 10
   time += 1
+17
source

time , time+=10 . C, while time, , .

+1

Your appointment timeon the last line is not affected. At the top of the loop, timeit is immediately assigned to the next value given range. But why is this a loop at all, can't you just do the calculations directly?

+1
source

All Articles