How to create a Countdown Timer in Python
If you need to count down a specific amount of time, say for example, when a token is set to expire, then a countdown timer will be useful.
Step 1 – Writing the Python code
Place the following Python code in a file called countdowntimer.py
Option 1 – Text output
Option 2 – Text and speech output
Here we also invoke say
which speaks out the text. This is only native to some operating systems.
Note: If you use MacOS, this will work out the box for you!
|
|
Step 2 – Calling the Python code
|
|
Optional Step 3 – Create an Alias
If you use bash
then do this:
|
|
If you use zsh
then do this:
|
|
Now once you’ve reloaded the terminal session by doing one of the following:
source ~/.bash_profile
source ~/.zshrc
- Close and reopen your terminal
You can just call count
and the application will start.
What input does it expect?
When you first run the application, you can specify the amount of seconds to count down from.
If you enter 30
, then it will count down for 30 seconds.
If you enter 3600
(60seconds x 60minutes), then it will count down for an hour.
etc.