繁体   English   中英

为什么此脚本不循环/仅运行一次?

[英]Why does this script not loop/only runs once?

这是我写的一个简单的Pomodoro计时器。 从理论上讲,它将无限运行,在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:吗?

谢谢!

evamvid

将来尝试从控制台运行它,以便您可以看到引发异常时它生成的回溯。

print("You have completed" + times + "Pomodoros.")

您不能隐式连接int和字符串。 这将引发TypeError ,从而结束程序。

修理:

print("You have completed " + str(times) + " Pomodoros.") # this works, and is ugly

print("You have completed {} Pomodoros.".format(times)) # better.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM