我在python中写倒计时但是它没有用。
for countdown in 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, quit():
print (countdown)
代码。倒计时只是放大并退出程序。有没有办法让倒计时符合实时和倒计时,并在10秒内退出。不是立即。
答案 0 :(得分:1)
tl; dr python缓冲区打印,而python不是 缓慢
import sys, time
for countdown in range(10,1,-1):
print(countdown)
sys.stdout.flush() #flushes buffer
time.sleep(1) #sleep for one second, otherwise loop goes very quickly
print(0)
sys.stdout.flush()
# no quit needed. Program will end when file ends
答案 1 :(得分:1)
你需要告诉它等待,否则它会像处理器一样快地运行代码。
from time import sleep
for countdown in range(10,0,-1):
print(countdown)
sleep(1)