Countdown timer | Python project 

Introduction :

Everyone requires a stopwatch, to calculate the time spent in any participation or some other cases. Every electronic device is included with a pre-installed stopwatch, but what if I tell you that, you can achieve the countdown timer just by a few lines of python code on your own, interesting right? In this tutorial, we’ll look into how we can achieve a countdown timer in python.

Outline :

A countdown timer is just a simple clock that counts the time in reverse from the starting point to zero.

A countdown timer is a virtual clock on the console that counts down from a certain number to indicate the beginning or end of an event or offer. This rushes the people to do the actions a quick and generates more conversions because “time is running out.” Countdown timers can also be used to count down to when an offer becomes available at a specific date and time.

Project Prerequisites : 

Time: The Python time module provides many ways of representing time in code, such as objects, numbers, and strings. It also provides functionality other than representing time, like waiting during code execution and measuring the efficiency of your code.

That’s it! You don’t have to install any other packages for this project.

Code Implementation : 

To understand this project, you should know about the following Python programming topics:

● Python while Loop
● Python divmod()
● Python time Module

The first thing we need to do is to import the time module.

#import time module for the further usage 
import time

We defined a function named countdown () and passed the input time as an argument.

See also  Alarm Clock Using Python

The divmod() method takes two numbers and returns a pair of numbers in the tuple data structure consisting of their quotient and remainder.

end=’\r‘ overwrites the output for each iteration, which gives the perfect look of a countdown timer.

The value of t, which is represented as an input of time, is decremented at the end of each iteration.

Python time method sleep() suspends execution for the given number of seconds. The argument may be a floating-point number to indicate a more accurate delay of time in each iteration.

The line in the code, {:02d}:{:02d}’.format(mins, secs) Indicates that there should be two in mins and two digits in seconds as the mins and secs are represented in the format() function.

while() loop is used to iterate the code until it becomes zero, which is the final destination of the countdown

here while loop is implemented as it continues until the time is greater than or equal to zero.
#while loop implementation
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        t -= 1

Source code :

Here is the complete source code for this project.

import time
def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        t -= 1
t = input("Enter the time in seconds: ")
print('Time's Up!')

Output :

You can run the source code and get the output like the following.

After the 30 seconds (entered input time), controls get out of the while loop, and “Time’s Up!” is printed.

Output of the above source code

So , that’s it !! This is the simplest method to create your own countdown timer . You can make slight modifications in the code , regarding your interest , to increase the look of the output

See also  Learn Intermediate Python: Best Youtube Playlists

Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.