Python Program to Create a Countdown Timer

Python Program to Create a Countdown Timer

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

We’ll look at how to make a countdown timer in Python. The code will accept the user’s input on the length of the countdown in seconds. Following that, a countdown in the format ‘minutes: seconds’ will commence on the screen. Here, we’ll make use of the time module.

Python Program to Create a Countdown Timer

Below are the ways to Create a Countdown Timer in Python.

Method #1: Using While Loop (Static Input)

The time module and its sleep() function will be used. To make a countdown timer, follow the steps below:

Approach:

  • Import the time module using the import statement.
  • Then in seconds, Give the length of the countdown as static input and store it in a variable.
  • This value is passed to the user-defined function countdownTime() as a parameter ‘tim’ .
  • A while loop is used in this function until the time reaches zero.
  • To calculate the number of minutes and seconds, use divmod().
  • Now, using the variable timeformat, output the minutes and seconds on the screen.
  • By using end = ‘\n ‘, we force the cursor to return to the beginning of the screen (carriage return), causing the following line written to overwrite the preceding one.
  • The time. sleep() function is used to make the program wait one second.
  • Now decrement time so that the while loop can reach a finish.
  • After the loop is completed, we will print “The Given time is completed” to indicate the conclusion of the countdown.

Below is the implementation:

# Import the time module using the import statement.
import time

# creating a function time which counts the time


def countdownTime(tim):
    # A while loop is used in this function until the time reaches zero.
    while (tim):
       # To calculate the number of minutes and seconds, use divmod(). 
        timemins, timesecs = divmod(tim, 60)
    # Now, using the variable timeformat, output the minutes and seconds on the screen.
        timercount = '{:02d}:{:02d}'.format(timemins, timesecs)
    # By using end = '\n', we force the cursor to return to the
    # beginning of the screen (carriage return),
    # causing the following line written to overwrite the preceding one.
        print(timercount, end="\n")
    # The time. sleep() function is used to make the program wait one second.
        time.sleep(1)
    # Now decrement time so that the while loop can reach a finish.
        tim -= 1
    # After the loop is completed, we will print "The Given time is completed" to indicate the conclusion of the countdown.
    print('The Given time is completed')


# Then in seconds, Give the length of the countdown as static input and store it in a variable.
tim = 5

# Passing the given time as argument to the countdownTime() function
countdownTime(tim)

Output:

00:05
00:04
00:03
00:02
00:01
The Given time is completed

Method #2: Using While Loop (User Input)

The time module and its sleep() function will be used. To make a countdown timer, follow the steps below:

Approach:

  • Import the time module using the import statement.
  • Then in seconds, Give the length of the countdown as user input and store it in a variable.
  • Convert the given time in seconds to integer data type using int() function.
  • This value is passed to the user-defined function countdownTime() as a parameter ‘tim’ .
  • A while loop is used in this function until the time reaches zero.
  • To calculate the number of minutes and seconds, use divmod().
  • Now, using the variable timeformat, output the minutes and seconds on the screen.
  • By using end = ‘\n ‘, we force the cursor to return to the beginning of the screen (carriage return), causing the following line written to overwrite the preceding one.
  • The time. sleep() function is used to make the program wait one second.
  • Now decrement time so that the while loop can reach a finish.
  • After the loop is completed, we will print “The Given time is completed” to indicate the conclusion of the countdown.

Below is the implementation:

# Import the time module using the import statement.
import time

# creating a function time which counts the time


def countdownTime(tim):
    # A while loop is used in this function until the time reaches zero.
    while (tim):
       # To calculate the number of minutes and seconds, use divmod(). 
        timemins, timesecs = divmod(tim, 60)
    # Now, using the variable timeformat, output the minutes and seconds on the screen.
        timercount = '{:02d}:{:02d}'.format(timemins, timesecs)
    # By using end = '\n', we force the cursor to return to the
    # beginning of the screen (carriage return),
    # causing the following line written to overwrite the preceding one.
        print(timercount, end="\n")
    # The time. sleep() function is used to make the program wait one second.
        time.sleep(1)
    # Now decrement time so that the while loop can reach a finish.
        tim -= 1
    # After the loop is completed, we will print "The Given time is completed" to indicate the conclusion of the countdown.
    print('The Given time is completed')


# Then in seconds, Give the length of the countdown as user input and store it in a variable.
tim = input('Enter some random time = ')
# Convert the given time in seconds to integer data type using int() function.
tim = int(tim)

# Passing the given time as argument to the countdownTime() function
countdownTime(tim)

Output:

Enter some random time = 4
00:04
00:03
00:02
00:01
The Given time is completed

Related Programs: