How to Make Countdown Timer for Your Next Run Using Python!

How to Make Countdown Timer for Your Next Run Using Python!

Hi there!

As you know, the purpose of a countdown timer is to create a sense of urgency and give the feeling that “time is running out.

Today, I'm going to create a Countdown Timer for Your Next Run. So, You RUN well. Your next run will be with Python Countdown Timer.

Before starting this, I'm going to tell you the difference between Countdown Timer and Stopwatch.
The Countdown timer starts from High Time, such as, 0:10 and goes like 0:09, 0:08, 0:07. A Stopwatch starts from Low Time such as, 0:01 and goes like 0:02, 0:03, 0:04.

Let's start the roadmap of what I'm going to do Using Python!

  • Show the timer in mins : secs format.
  • Time should start from second unit.
  • Time should display as Countdown Timer(0 : 10 =>0 : 09) format.

To do this, I'm going to use the following things in Python:

  • Import the time module.
  • Python function take the time as a parameter and repeat the Instructions.
  • While Loop to Execute set of Instruction until a time not equal to 1sec.
  • Use the input() method to take user input.
  • Call the function with the given time.

Import the time module

Python Use time module to work with time-related tasks.
To use this module, you need to import it. Here's how :

import time

Here are time related function :

  • time.time()
    This module return time in seconds from epoch(Where Time starts).
    Epoch for Windows and Unix System is January 1, 1970, 00:00:00 at UTC.
    Here's how :
import time
seconds = time.time()
print("Seconds since epoch for window system:",seconds)

Output:

Seconds since epoch: 1642400120.553795

  • time.ctime()

It's the opposite of the time.time() function. You can pass seconds since epoch as a parameter. it'll return local time. Such as, Mon Jan 17 11:45:20 2022
Here's how :

import time
local_time = time.ctime(1642400120.553795)
print("Time in local format",local_time)

Output:

Time in local format Mon Jan 17 11:45:20 2022

  • time.sleep()

This method suspends the execution of the program for seconds.
It takes second as a parameter and suspends the execution for seconds.
Thus, you can halt the program's execution for seconds.
Here's how :

# Python code to demonstrate
# working of sleep()

import time

local_time = time.ctime()
# printing the start time
print("The time of code execution begin is : ", local_time)

# using sleep() to halt the code execution
time.sleep(6)

l = time.ctime()
# printing the end time
print("The time of code execution end is : ",l)

Output:

The time of code execution begin is :  Tue Jan 18 09:13:40 2022
The time of code execution end is :  Tue Jan 18 09:13:46 2022

You can see that code execution begin at Tue Jan 18 09:13:40 2022.
And end at Tue Jan 18 09:13:46 2022.
The difference of 6 seconds in Python code execution.
Hence, you can delay the program execution using time.sleep() method.

So, I've told you about three pythons time-related functions.
In Countdown Timer, as you know we've to go to the previous second.
Thus, I'm going to use time.sleep() method to halt time for one second.

Define the Python Function

Before Using time.sleep() I've to define the function. The function executes the set of Instructions repeatedly. It takes t(times in second) as a parameter.

Note: t in seconds format.

import time
def countdown(t):

Use Python While loop

Because we've to display Countdown Timer until t not equal to one second, I'm going to use while looping.
When t = 1 second timer finish and Python Interpreter end the While loop. Let's Use the While loop:

import time
def countdown(t):
    while t:

Inside while loop, I'm going to show the time in seconds.
See below:

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)

Note: The divmod() returns remainder and quotient. Here remainder -> mins and Quotient -> seconds.

In next line of code, I've to show the Countdown Timer format.
To format the time, you can use Python String Format .

Here's how :

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"

In the last line, You can see that I've formatted the Countdown Timer using Python fstring.
The Other option to format the Countdown Timer is '{:02d}:{:02d}'.format(mins, secs). Which is Python 2.x string formatting method.

You can observe, I've used 2d in both methods. The 2d means formats to 2 characters using padding. Because we need time in Integer format.
We need to display this time using the print() method.
See below:

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")

In print(timer,end = "\r") line, I've used \r . Which is carriage return "\r" to print over the same line. Because we need to display every second in same line.

As you know, previously I've told you about time.sleep() method.
This is the time to use it.
We'll use it to suspend execution for 1 second.
Here's how :

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")
        # Program wait for 1 second
        time.sleep(1)

I've used time.sleep() method because in Countdown Timer time decreases for 1 second. So, we need to halt the execution for 1 second.

As you know, Countdown Timer goes from high-seconds to low-second. So, we need to decrease t by 1.
Here's how:

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")
        # Program wait for 1 second
        time.sleep(1)
        t-=1

When while loop end, we need to display the " timer completed! " statement. See below:

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")
        # Program wait for 1 second
        time.sleep(1)
        t-=1
   print("timer completed!")

Use Python input() method

The Second Last part that I'm going to Use is to take input from the User.
A user gives the input using the input() method.
See below code:

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")
        # Program wait for 1 second
        time.sleep(1)
        t-=1
   print("timer completed!")

t = input("Enter the time in seconds:")

So, we've given all the things for this project.

Call the Python function

The last part is that we've to call the function to execute the program.
The countdown(int(t)) line call the function and executes the program. Here's how :

import time
def countdown(t):
    while t:
        # This showing the time in seconds
        mins,secs = divmod(t, 60)
        # second I've to format the mins, secs time format
        # 2d means formats to 2 characters using padding
        timer = f"{mins}:{secs:02d}"
        # \r: To make next time into beginning of the current line
        print(timer,end = "\r")
        # Program wait for 1 second
        time.sleep(1)
        t-=1
   print("timer completed!")

t = input("Enter the time in seconds:")
countdown(int(t)

Output:

Enter the time in seconds:10

After clicking the enter button. You'll something like below: Countdown Timer

When timer becomes 0 : 01. You'll see " timer completed! " message.
Absolutely, not here because it's a Gif timer.

Benefits of Using Countdown Timers

  • You can put it into your website to create urgency.
  • In Email Marketing, you can set with e-mail. So, the user can see the ending of the sale.
  • To do the Important task, you can set the Countdown Timer.
  • Running is the part of many lives. So, you can use Countdown Timer to achieve the extra
    mile.
  • Highlight the product using Countdown Timer.
  • Digital Countdown timer use in Hospital Operating room.

Best Apps for Countdown Timer

You can get the best Countdown Timer here.
Tell me in the Comment Section which one is your favorite Countdown App.

Online Countdown Timer

As you know, we love everything Online these days. Hence you can use timeanddate.com an online timer.

Conclusion:

Finally, you've seen the Countdown Timer Using Python.
To use Timer, I've imported the time module. This module handles Python time-related functions.
From it, You've to use time.sleep() function to suspend the execution for seconds.

Likewise, I've used the python function to repeat the set of instructions.
These instructions are present inside the while loop.
The while loop executes the instructions till the timer is running. When timer finishes running, it shows " timer complete! " message.

Meanwhile, We've to show this Timer on Screen. For that, I've used " Python String format ". So, When Countdown Timer run. It'll show every second of time in one line.
For this purpose, I've used Carriage Return("\r") in print statement.
It'll display every second of time in the same line.

We need intervals between two seconds of time to display every second. This interval can provide using time.sleep() function. And every second decreases by 1 second.

Outside of the Python function, I've given user Input and called the function.
When you call the function, user Input pass through the Python function. And Python starts the Countdown Timer.
And display the "timer complete!" message.

Also, I've told you some benefits of Countdown Timer.
You can provide another benefits in Comment Section.


Do you know you can help me?

Here's how:
You can read my python projects and rate them in Comment Section.
I'll edit the projects that have ratings less than 4 out of 5.
So, you can get efficient and good projects. Not boring projects!

Check out below projects to rate them :

below sign

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!