DEV Community

loading...
Cover image for Python Scripts- Countdown Timer

Python Scripts- Countdown Timer

envoy_ profile image Envoy-VC Updated on ・1 min read

This Script will take an input from the user in form of time and play a sound when the timer is over

Required Packages - playsound
to install it run this command on Terminal/PowerShell

pip install playsound
Enter fullscreen mode Exit fullscreen mode

Input should be in format of MM:SS eg-

30:00
07:30
Enter fullscreen mode Exit fullscreen mode



CODE -

import time
from playsound import playsound

def countdown(t):

    min_sec = input_time.split(sep=':')
    time_in_sec = (int(min_sec[0]) * 60) + int(min_sec[1])
    while time_in_sec:
        mins, secs = divmod(time_in_sec,60)
        timer = f"{mins}:{secs}"
        print('\r', timer, end="")
        time.sleep(1)
        time_in_sec -= 1

    playsound('audio.wav')

input_time = input(print("Enter Time (MM:SS) - "))
countdown(input_time)
Enter fullscreen mode Exit fullscreen mode

Source Code Here
Visit my Blog Here

Discussion (0)

pic
Editor guide