如何循环范围()函数x在Python中有时间延迟的次数? 例如,对于范围内的i(4,9),重复3次,每次在开始新计数前停止5秒。答案应该是这样的:
"0 second delay" (4,5,6,7,8,9)
"5 second delay" (4,5,6,7,8,9)
"10 second delay" (4,5,6,7,8,9)
答案 0 :(得分:2)
这个怎么样?
from time import sleep
N = 3 # number of times you want to repeat
for d in xrange(N):
sleep(d * 5)
for x in xrange(4, 10):
# do something
注意:假设Python 2.x(xrange),则应使用常规范围(Python 3.x)。