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, all the way down to zero.

Steps

  1. 1
    Open your text editor or IDE. On Windows, the easiest option is to use IDLE, which is installed together with Python.
  2. 2
    Open a new file. In many text editors, you can do this by going to the file menu and click on New Window or by just pressing Ctrl+N.
    Advertisement
  3. 3
    Import the time module. 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. 4
    Define a countdown function. 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. 5
    Write a while-loop. A while-loop repeats the code inside it as long as 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 that 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. 6
    Print the current number. 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. 7
    Count down the number. 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. 8
    Make the program wait a second. 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. 9
    Do something when the countdown reaches zero. 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. 10
    Ask the user from which number to start the countdown. 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 that 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 the reason while the program checked whether the answer was actually an integer first.
  11. 11
    Call the countdown() function. 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. 12
    Check your finished code. 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 actually ignores them.
    • You can write t = t - 1 instead of t -= 1 if you prefer.
  13. Advertisement

Community Q&A

  • Question
    How do I get it to print at each second rather than having it all print at once?
    Community Answer
    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
    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
    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.
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 26 people, some anonymous, worked to edit and improve it over time. This article has been viewed 186,731 times.
How helpful is this?
Co-authors: 26
Updated: March 16, 2023
Views: 186,731
Categories: Python
Advertisement