2 回答

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

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
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
...
添加回答
舉報(bào)