In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format ‘minutes: seconds’. We will use the time module here.
In this project, we will be using the time module and its sleep() function. Follow the below steps to create a countdown timer:
- Step 1: Import the time module.
- Step 2: Then ask the user to input the length of the countdown in seconds.
- Step 3: This value is sent as a parameter ‘t’ to the user-defined function countdown(). Any variable read using the input function is a string. So, convert this parameter to ‘int’ as it is of string type.
- Step 4: In this function, a while loop runs until time becomes 0.
- Step 5: Use divmod() to calculate the number of minutes and seconds. You can read more about it here.
- Step 6: Now print the minutes and seconds on the screen using the variable timeformat.
- Step 7: Using end = ‘\r’ we force the cursor to go back to the start of the screen (carriage return) so that the next line printed will overwrite the previous one.
- Step 8: The time.sleep() is used to make the code wait for one sec.
- Step 9: Now decrement time so that the while loop can converge.
- Step 10: After the completion of the loop, we will print “Fire in the hole” to signify the end of the countdown.
Below is the implementation of the above approach
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Create Countdown Timer using Python-Tkinter
- Set Countdown timer to Capture Image using Python-OpenCV
- Python Program to Create a Lap Timer
- Timer Application using PyQt5
- How to show a timer on screen using arcade in Python3?
- Timer Objects in Python
- PyQt5 QCalendarWidget - Setting Start Timer
- PyQt5 QCalendarWidget - Setting Timer Event
- PyQt5 QCalendarWidget - Killing the timer
- wxPython - Create Radio Button using Create() function
- wxPython - Create Static Box using Create() method
- Python | Create video using multiple images using OpenCV
- Python | Create a stopwatch using clock object in kivy using .kv file
- Create a Website Alarm Using Python
- Create simple Blockchain using Python
- Create a stopwatch using python
- Python | Create and write on excel file using xlsxwriter module
- Create XML Documents using Python
- Python | Create a dictionary using list with none values
- Python | Create a digital clock using Tkinter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.