【发布时间】:2014-03-13 05:32:13
【问题描述】:
这是我写的一个简单的番茄钟。理论上,它会无限运行,在 25 分钟和 5 分钟之间交替计时。
import time
import sys
import datetime
import winsound
def ring(sound):
winsound.PlaySound('%s.wav' % sound, winsound.SND_FILENAME)
times = 0
while True:
tomato = 0
while tomato <= 1500:
timeLeft = 1500 - tomato
formatted = str(datetime.timedelta(seconds=timeLeft))
sys.stdout.write('\r'+ formatted)
time.sleep( 1 )
tomato += 1
ring("MetalGong")
fun = 0
while fun <= 300:
funTimeleft = 300 - fun
funFormatted = str(datetime.timedelta(seconds=funTimeleft))
sys.stdout.write('\r'+ funFormatted)
time.sleep( 1 )
fun +=1
ring("AirHorn")
times += 1
print("You have completed" + times + "Pomodoros.")
但是,它只通过了一次;完成 5 分钟块后,控制台窗口立即关闭(我直接通过双击运行它,而不是通过终端)。
为什么会这样关闭?和我使用while True:的方式有关系吗?
谢谢!
伊夫维德
【问题讨论】:
-
如果你从控制台运行它,你会很快看到为什么......在大约 5 分钟内
-
看起来它在这个 'winsound.PlaySound('%s.wav' % sound, winsound.SND_FILENAME)' 行中失败了。您收到任何错误消息吗?
标签: python loops python-2.7 while-loop