How To Create a Countdown Timer Using Python?

In this article, we will perceive how to make a commencement clock utilizing Python. The code will take input from the client in regards to the length of the commencement like a flash. From that point onward, a commencement will start on the screen of the arrangement ‘minutes: seconds’. We will utilize the time module here.

How To Create a Countdown Timer Using Python?

Approach

In this task, we will utilize the time module and its rest() work. Follow the underneath steps to make a commencement clock:

Stage 1: Import the time module.
Stage 2: Then request that the client input the length of the commencement in a flash.
Stage 3: This worth is sent as a boundary ‘t’ to the client characterized work commencement(). Any factor read utilizing the info work is a string. Along these lines, convert this boundary to ‘int’ all things considered of string type.
Stage 4: In this capacity, some time circle runs until time becomes 0.
Stage 5: Use divmod() to compute the quantity of minutes and seconds. You can peruse more with regards to it here.
Stage 6: Now print the minutes and seconds on the screen utilizing the variable timeformat.
Stage 7: Using end = ‘\r’ we power the cursor to return to the beginning of the screen (carriage return) with the goal that the following line printed will overwrite the past one.
Stage 8: The time.sleep() is utilized to make the code hang tight for one sec.
Stage 9: Now decrement time so that the while circle can join.
Stage 10: After the consummation of the circle, we will print “Fire in the opening” to mean the finish of the commencement.

Time following is basic to deal with your ventures. In the event that you’re customizing in Python, fortune has smiled on you: The language gives you apparatuses to assemble your own clocks. A clock program can be helpful for observing your own time, however estimating how long your Python program requires to run.

In this article, we’ll introduce two straightforward Python clocks prior to checking out the modules you’ll have to make a clock program. We’ll then, at that point, utilize these modules to construct a stopwatch and a commencement clock, and tell you the best way to quantify a Python program’s execution time.

Understanding Timers in Python

A clock in Python is a period following system. Python designers can make clocks with the assistance of Python’s time modules. There are two fundamental kinds of clocks: clocks that count up and those that count down.

Stopwatches

Clocks that count up from zero are often called stopwatches. We can utilize these to record how much time it takes to do a responsibility.

Sprinters regularly use stopwatch clocks to record what amount of time it requires for them to finish a lap around a course or finish a whole run. You can utilize a stopwatch to follow what amount of time it requires to do any job, like coding a basic program.

Developers regularly use stopwatch clocks to think about the presentation of different Python arrangements. By perceiving what amount of time every arrangement requires to execute, you can pick the program that runs the quickest. After some time, this will permit you to more readily comprehend computational intricacy and fabricate instinct for picking productive arrangements. These abilities go far towards turning into a capable software engineer.

Commencement Timers

There are likewise commencement clocks, set with a particular measure of time that exhausts until the clock arrives at nothing. You can utilize Python to assemble commencement clocks that fill various needs.

For example, you can assemble a cooking commencement clock to guarantee you don’t leave food in the stove for a really long time. Commencement clocks can likewise attempt to show the time staying in a game or during a test. They are likewise used to count during the time and minutes to a film discharge or large occasion. The prospects are unending!

Modules in Python

  • To make a straightforward clock in Python, you’ll have to call upon Python’s time and datetime modules.
  • Modules in Python are records that contain classes, capacities, factors, and runnable code. By bringing a module into your program, you can utilize every part inside the module.
  • The Python programming language has north of 200 predefined modules inside its standard library. You can even make your own modules to use in later projects.

The Python Time Module

One of the 200 modules in Python’s standard library is the time module. This module contains the capacities we’ll have to fabricate a straightforward clock in Python.

To involve the time module in Python, we initial import it into our program:

Posting modules toward the start of a Python program makes the capacities inside the module accessible for use in our program. The time module holds a larger number of capacities than we’ll cover here, however here are the main ones:

Countdown time in Python

import time

def countdown(time_sec):
    while time_sec:
        mins, secs = divmod(time_sec, 60)
        timeformat = '{:02d}:{:02d}'.format(mins, secs)
        print(timeformat, end='\r')
        time.sleep(1)
        time_sec -= 1

    print("stop")

countdown(5)
  • The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.
  • end='\r' overwrites the output for each iteration.
  • The value of time_sec is decremented at the end of each iteration.

Also ReadHow to print exception stack trace in Python?

Leave a Reply

Your email address will not be published.