How to Create a Countdown Timer in Python
In this tutorial, we will program 'How to Create a Countdown Timer in Python'. We will learn how to create a proper countdown timer. The main goal here is to create a reliable countdown timer that can be used to time any events. I will provide a sample program to demonstrate the actual coding of this tutorial.
This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for creating a countdown timer. I will do my best to provide you with the easiest method of creating this countdown timer program. So, let's start with the coding.
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display the countdown timer. To do this, simply copy and paste these blocks of code into the IDLE text editor.- 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')
- print("\n")
- time.sleep(1)
- time_sec -= 1
- print("Timer end")
- while True:
- ret=False
- print("\n\n=============== Countdown Timer ===============\n\n")
- sec=int(input("Set your timer in sec: "))
- countdown(sec)
- user_input=input("\n\nTry Again? (Yes/No): ")
- if user_input.lower()=='no':
- print("Exit program.")
- break
- elif user_input.lower()=='yes':
- ret=True
- else:
- print("Wrong input.")
- ret=True
- if ret:
- continue
In the provided code, we simply enclosed the code with a while loop to allow for continuous execution of the program. We will create a method that will set the timer based on the input you provide. You just need to call the method in order to display the timer.
Output:
The How to Create a Countdown Timer in Python source code that I provide can be download below. Please kindly click the download button.
There you have it we successfully created How to Create a Countdown Timer in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language
Add new comment
- 141 views