X
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.
Learn more...
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
-
1Open your text editor or IDE. On Windows, the easiest option is to use IDLE, which is installed together with Python.
-
2Open 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
-
3Import the
time
module. Thetime
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
-
4Define 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):
-
5Write 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.
- Notice the spaces at the beginning of the line. These tell Python that this line of code is part of the definition of the
-
6Print 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)
-
7Count 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
-
8Make 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 thetime
module that you had previously imported:time.sleep(1)
-
9Do 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.
-
10Ask 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.
- Print the question to the user. They need to know what they are supposed to enter.
-
11Call 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 thecountdown()
function with the number of seconds that the user inputted:countdown(seconds)
-
12Check 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.
Advertisement
Community Q&A
-
QuestionHow do I get it to print at each second rather than having it all print at once?Community AnswerUse the time.sleep(x) function. It allows for the program to pause for x seconds. After every print statement, insert time.sleep(1).
-
QuestionHow do I make the font larger in Python on a Mac?Community AnswerIn the Python shell, click Options, Configure, Idle. From there, you can change the font size.
-
QuestionWhy have the 'time' module if it is never used?Community AnswerIf 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
Advertisement