PDF download Download Article
Learn how to write a simple countdown timer in Python
PDF download Download Article

If you're just getting started with Python, making a countdown time is a great way to exercise your programming skills. We'll show you how to write a Python 3 program that counts down from any number down to zero.

Creating a Countdown Timer in Python

Start by importing the time module and define the countdown function. Write a while-loop, make the program print the current number, make the timer go down by 1, and use sleep() to make the timer not count down so fast. Add a field for the user to enter their number, and finally, call the countdown function.

  1. On Windows, the easiest option is to use IDLE, which is installed with Python.
  2. In many text editors, you can do this by going to the file menu and clicking on New Window or pressing Ctrl + N .
    Advertisement
  3. The time contains many Python functions related to time, for example, getting the current time or waiting a specified amount of time (the latter is what you will need for this program). To import the module, type:
     import 
     time 
    
  4. You can give the function any name you want, but usually, you should use something descriptive. In this case, you could name it countdown() . Add the following code:
     def 
     countdown 
     ( 
     t 
     ): 
    
  5. A while loop repeats the code inside it if its condition is true. In this case, you want the countdown to continue until the number reaches 0. So, you need to write:
     while 
     t 
     > 
     0 
     : 
    
    • Notice the spaces at the beginning of the line. These tell Python that this line of code is part of the definition of the countdown function and not just some code below it. You can use any number of spaces, but you need to use the same amount before any line you want to indent once.
    • You will need to indent the next code lines twice because they are both part of the function definition and part of the while-loop. This is done by using twice as many spaces.
  6. This does not mean using a printer to get it on paper, "printing" is a word that means "displaying on the screen". This will let you see how far the countdown has progressed.
     print 
     ( 
     t 
     ) 
    
  7. Make it 1 less. This is done with the following code:
     t 
     = 
     t 
     - 
     1 
    

    Alternatively, if you don't want to type so much, you can instead write:
     t 
     -= 
     1 
    
  8. Otherwise, it would be counting down the numbers way too fast, and the countdown would be finished before you could even read it. For waiting a second, use the sleep function of the time module that you had previously imported:
     time 
     . 
     sleep 
     ( 
     1 
     ) 
    
  9. To print out "BLAST OFF!" when the countdown reaches zero, add this line:
     print 
     ( 
     "BLAST OFF!" 
     ) 
    
    • Note that this line is only indented once . This is because it is no longer part of the while loop. This code is only run after the while loop finishes.
  10. This will give your program some flexibility instead of always counting from the same number.
    • Print the question to the user. They need to know what they are supposed to enter.
       print 
       ( 
       "How many seconds to count down? Enter an integer:" 
       ) 
      
    • Get the answer. Store the answer in a variable so that you can do something with it later.
       seconds 
       = 
       input 
       () 
      
    • While the user's answer is not an integer, ask the user for another integer. You can do this with a while loop. If the first answer is already an integer, the program will not enter the loop and just proceed with the next code.
       while 
       not 
       seconds 
       . 
       isdigit 
       (): 
       print 
       ( 
       "That wasn't an integer! Enter an integer:" 
       ) 
       seconds 
       = 
       input 
       () 
      
    • Now you can be sure the user entered an integer. However, it is still stored inside a string ( input() always returns a string because it can't know whether the user will enter text or numbers). You need to convert it to an integer:
       seconds 
       = 
       int 
       ( 
       seconds 
       ) 
      

      If you would have tried to convert a string whose content isn't an integer into an integer, you would get an error. This is why the program checked whether the answer was an integer first.
  11. You had previously defined it, but defining a function doesn't do what is written inside it. To actually run the countdown code, call the countdown() function with the number of seconds that the user inputted:
     countdown 
     ( 
     seconds 
     ) 
    
  12. It should look like this:
     import 
     time 
     def 
     countdown 
     ( 
     t 
     ): 
     while 
     t 
     > 
     0 
     : 
     print 
     ( 
     t 
     ) 
     t 
     -= 
     1 
     time 
     . 
     sleep 
     ( 
     1 
     ) 
     print 
     ( 
     "BLAST OFF!" 
     ) 
     print 
     ( 
     "How many seconds to count down? Enter an integer:" 
     ) 
     seconds 
     = 
     input 
     () 
     while 
     not 
     seconds 
     . 
     isdigit 
     (): 
     print 
     ( 
     "That wasn't an integer! Enter an integer:" 
     ) 
     seconds 
     = 
     input 
     () 
     seconds 
     = 
     int 
     ( 
     seconds 
     ) 
     countdown 
     ( 
     seconds 
     ) 
    
    • The empty lines are only there to make the code easier to read. They are not required, and Python ignores them.
    • You can write t = t - 1 instead of t -= 1 if you prefer.
  13. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do I get it to print at each second rather than having it all print at once?
    Community Answer
    Use the time.sleep(x) function. It allows for the program to pause for x seconds. After every print statement, insert time.sleep(1).
  • Question
    How do I make the font larger in Python on a Mac?
    Community Answer
    In the Python shell, click Options, Configure, Idle. From there, you can change the font size.
  • Question
    Why have the 'time' module if it is never used?
    Community Answer
    If you write a program for, say, a robot and have the servo controls in milliseconds, then it will use the time module to send the electrical signal for the right amount of time.
See more answers
Ask a Question
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!

      About This Article

      Thanks to all authors for creating a page that has been read 203,040 times.

      Is this article up to date?

      Advertisement