• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Countdown Timer in Python

Saltyu

Mood Booster
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
223
Likes
62
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.
  1. import

    time

  2. def

    countdown(

    time_sec)

    :
  3. while

    time_sec:
  4. mins,

    secs =

    divmod

    (

    time_sec,

    60

    )
  5. timeformat =

    '{:02d}:{:02d}'

    .format

    (

    mins,

    secs)
  6. print

    (

    timeformat,

    end=

    '\r

    '

    )
  7. print

    (

    "\n

    "

    )
  8. time

    .sleep

    (

    1

    )
  9. time_sec -=

    1

  10. print

    (

    "Timer end"

    )


  11. while

    True

    :
  12. ret=

    False

  13. print

    (

    "\n

    \n

    =============== Countdown Timer ===============\n

    \n

    "

    )


  14. sec=

    int

    (

    input

    (

    "Set your timer in sec: "

    )

    )

  15. countdown(

    sec)


  16. user_input=

    input

    (

    "\n

    \n

    Try Again? (Yes/No): "

    )
  17. if

    user_input.lower

    (

    )

    ==

    'no'

    :
  18. print

    (

    "Exit program."

    )
  19. break

  20. elif

    user_input.lower

    (

    )

    ==

    'yes'

    :
  21. ret=

    True

  22. else

    :
  23. print

    (

    "Wrong input."

    )
  24. ret=

    True


  25. if

    ret:
  26. 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:

how-to-create-a-countdown-timer-in-python-1.png

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

Python Tutorials


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top