簡體   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