我想做一些事情之后iis 20,40等等。例如:i = 0while True: i+=1 if i == # increment of 20 (40, 60, 80, etc.): # do this
2 回答
![?](http://img1.sycdn.imooc.com/545868b60001587202200220-100-100.jpg)
翻阅古今
TA贡献1780条经验 获得超5个赞
选项 1 和 2 使用模运算符来检测何时i是 的乘法20,但效率较低,因为会发生不必要的迭代。
选项 3 使用range, 并且效率更高,因为只会发生必要的迭代。
选项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 循环
使用范围:从 开始20直到threshold跳跃20
threshold = 10000
for i in range(20, threshold, 20):
print(i)
![?](http://img1.sycdn.imooc.com/5333a1660001394602000200-100-100.jpg)
阿波罗的战车
TA贡献1862条经验 获得超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
提交
取消