Changing the number of iterations in a for loop

前端 未结 8 1352
死守一世寂寞
死守一世寂寞 2020-12-10 11:27

I have code like this:

loopcount = 3
for i in range(1, loopcount)
   somestring = \'7\'
   newcount = int(somestring)
   loopcount = newcount
8条回答
  •  温柔的废话
    2020-12-10 11:56

    It looks like your premise is that you have a default number of times the loop should execute but an occasional condition where it's different. It might be better to use a while loop instead, but regardless you can just do:

    if i == some_calculated_threshold:
        break
    

    to drop out of the loop instead.

提交回复
热议问题