簡體   English   中英

如何在 Python 中制作定時器?

[英]How can i make a Timer in Python?

我嘗試在 python 中制作一個計時器,但它不起作用。 每次都有錯誤信息

from time import sleep
import os

def countdown(time = 5):
    while time != 0:
        print(time)
        sleep(1)
        os.system('clear')
        time -= 1
        if time == 0:
            print("Time is going up")

time = input('Input number of seconds to countdown: ')
countdown(time)

錯誤信息:

TypeError: unsupported operand type(s) for -=: 'str' and 'int'

有人可以幫我解決這個問題嗎?

你的問題是 integer 和字符串類型混合

這是工作正常的代碼:

from time import sleep
import os

def countdown(time = 5):
    while time != 0:
        print(time)
        sleep(1)
        os.system('clear')
        time -= 1
        if time == 0:
            print("Time is going up")

time = int(input('Input number of seconds to countdown: '))
countdown(time)

我剛剛將time存儲為int類型(您存儲的是字符串類型)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM