请考虑以下代码:
i = 0
while True:
# Do some other stuff first.
# Check if this iteration is after 7.
i += 1
if i % 7 == 0: print 'Factor of 7'
这个很好,但是柜台
i
迟早会是一个巨大的数字。有没有更好的方法来做一些长期的每x(在上面的例子中,每7次)迭代,这样我们就不必存储大量的数据了?我想到了以下几点:
i = 0
while True:
# Do some other stuff first.
# Check if this iteration is after 7.
i += 1
if i % 7 == 0:
i = 0
print 'Factor of 7'
但似乎有更好的办法。有什么建议吗?