慕尼黑8549860
2023-06-27 18:30:13
我想做一些事情之后iis 20,40等等。例如:i = 0while True: i+=1 if i == # increment of 20 (40, 60, 80, etc.): # do this
2 回答

翻閱古今
TA貢獻1780條經(jīng)驗 獲得超5個贊
選項 1 和 2 使用模運算符來檢測何時i是 的乘法20,但效率較低,因為會發(fā)生不必要的迭代。
選項 3 使用range, 并且效率更高,因為只會發(fā)生必要的迭代。
選項1
用途not i % 20:
i = 0
while True:
i+=1
if not i % 20:
print(i)
選項2
用途0 == i % 20:
i = 0
while True:
i+=1
if 0 == i % 20:
print(i)
選項 3:For 循環(huán)
使用范圍:從 開始20直到threshold跳躍20
threshold = 10000
for i in range(20, threshold, 20):
print(i)

阿波羅的戰(zhàn)車
TA貢獻1862條經(jīng)驗 獲得超6個贊
i = 0
while True:
i+=1
if i % 20 == 0: # increment of 20 (40, 60, 80, etc.):
print(i) #or something else
輸出:
20
40
60
80
...
添加回答
舉報
0/150
提交
取消