How to make a timer program in Python

前端 未结 11 2238
予麋鹿
予麋鹿 2021-02-08 14:24

Here is my goal: To make a small program (text based) that will start with a greeting, print out a timer for how long it has been since the last event, and then a timer for the

11条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 15:17

    a simple timer program that has sound to remind you would be:

    from time import sleep
    import winsound
    m = 0
    print("""**************************
    Welcome To FASTIMER®
    **************************""")
    while True:
        try:
            countdown = int(input("How many seconds:  "))
            break
        except ValueError:
            print("ERROR, TRY AGAIN")
    original = countdown
    while countdown >= 60:
        countdown -= 60
        m += 1
    for i in range (original,0,-1):
        if m < 0:
            break
        for i in range(countdown,-2,-1):
            if i % 60 == 0:
                m-=1
            if i == 0:
                break
            print(m," minutes and ",i," seconds")
            sleep(1)
        if m < 0:
            break
        for j in range(59,-1,-1):
            if j % 60 == 0:
                m-=1          
            print(m," minutes and ",j," seconds")
            sleep(1)
    print("TIMER FINISHED")
    winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
    

    this program uses time.sleep() to wait a second. It converts every 60 seconds to a minute. the sound only works with Windows or you can install pygame to add sounds.

提交回复
热议问题