Python Program to Create a Countdown Timer

Dear Friends, Today we are going to learn how to create a Python Program for a countdown timer. This post is a step by step guide to create a time countdown with python Source code and program output. This Python program is for mid-level programmers, who have the basic knowledge of Python language. Moreover this is useful for Project File and Lab Assignment for Computer Science Engineering Students.

Step by Step Guide – Countdown Timer

Step 1: Import following three Files :
sys module is imported for shutdown in the case of lengthy program as per the wish of programmer.
Time to calculate that 1 second is passed and decreasing the left over time period.
Sevseg module for seven segment display counter. Here sevseg module has to be install in the same folder as the same program.

Step 2 : Assigning the value from where you wish to start the countdown timer to secondstocount variable in seconds. Here we have provided 3 seconds.

Step 3: Exception handling is used for shutting down the program when ctrl-c is pressed.

Step 4: A while loop in which the coming iteration is less than 1 second.

Step 5: In this loop secondstocount is converted in hours, minute, seconds and then displayed on seven segment display created in the output of the program.

Step 6: The loop is break when the 0 hour, 0 minute, 0 second is encountered and Fire is printed.

Source code : Program of countdown timer in Python

"""Countdown , by Archit
 Here, In the output we are showing seven segment display for a countdown.
 Press Ctrl-C to stop.
 It is Required sevseg.py to be in the same folder.
 """
 
import sys, time
import sevseg # Importing our sevseg.py program.

 # can be changed to any number of seconds to count from
secondstocount = 3

try:
    while True: # Main programing loop.
        # Clearing the screen by printing several newlines:
        print('\n' * 60)

        # Getting the hours/minutes/seconds from secondstocount:
        # For example: 7261 is 2 hours, 1 minute, 1 seconds.
        # So 7261 // 3600 is 2 hours:
        hours = str(secondstocount // 3600)
        # 7261 % 3600 is 65, and 65 // 60 is 1 minute:
        minutes = str((secondstocount % 3600) // 60)
        # 7261 % 60 is 1 seconds:
        seconds = str(secondstocount % 60)

        # Getting the digit strings from the sevseg module:
        hDigits = sevseg.getSevSegStr(hours, 2)
        hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines()

        mDigits = sevseg.getSevSegStr(minutes, 2)
        mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines()

        sDigits = sevseg.getSevSegStr(seconds, 2)
        sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines()
        # Displaying the digits:
        print(hTopRow + ' ' + mTopRow + ' ' + sTopRow)
        print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow)
        print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow)

        if secondstocount == 0:
            print()
            print(' * * * * FIRE * * * *')
            break

        print()
        print('Press Ctrl-C to quit.')

        time.sleep(1) # Insert a one-second pause.
        secondstocount -= 1
except KeyboardInterrupt:
    print('Countdown, by Archit')
    sys.exit() # When Ctrl-C is pressed, it ends the program.)

Output: Countdown timer program in Python

Here we have provided secondstocount to 3 . So, it will be showing the countdown from 3 to 0 seconds. We are fee to write in the source code about the second countdown we need.

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
====== RESTART: C:/Users/ARCHIT/Desktop/new python progrm/countdown.py ======

 __   __   __   __   __   __ 
|  | |  | * |  | |  | * |  |  __|
|__| |__| * |__| |__| * |__|  __|

Press Ctrl-C to quit.

 __   __   __   __   __   __ 
|  | |  | * |  | |  | * |  |  __|
|__| |__| * |__| |__| * |__| |__ 

Press Ctrl-C to quit.

 __   __   __   __   __      
|  | |  | * |  | |  | * |  |    |
|__| |__| * |__| |__| * |__|    |

Press Ctrl-C to quit.

 __   __   __   __   __   __ 
|  | |  | * |  | |  | * |  | |  |
|__| |__| * |__| |__| * |__| |__|

 * * * * FIRE * * * *
>>> 

Conclusion :

The Python Program to Create a Countdown Timer is running fine. This program is one of the most useful program in the industry. This program is used in timers and internet of thing devices. This program is in our smartphone and upcoming devices. These modules included in this program are of great use.

For more projects and programs click below:
Python Programs Archives – CS Study

Leave a Comment

Your email address will not be published.