/* jquery */ /* jquery accordion style*/ /* jquery init */

2 April 2013

Learn Python - A Timer App

Now we know what a while loop is and how it works, let's create a useful little timer program.

A timer program can be used for all sorts of activities. Maybe you want to time the moves in a game of chess, or cook the perfect hard boiled egg at breakfast. Knowing how to create a timer is an important programming skill, especially in games where there are many time-sensitive aspects.

Let's get started. Open the Geany editor and creating a new file using Geany's File->New menu option.

Before starting to type save this file into the Python folder we created on the Desktop last time. Save it as timer.py.

Now we need to type in the code. Check your typing as you go, making sure you add the colon : character and indent the loop lines by the same number of spaces.

It's a good idea to save your typing frequently, say after every few lines, so you don't lose any changes. If you use the shortcut Ctrl-S (control key plus the s key) it only takes a second.

Here's the code listing (note the lines numbers are for reference only, and not part of the code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# A timer countdown program
# Created by David Briddock - July 2012

import time

# get the countdown time in seconds
seconds = input("Number of seconds: ")

startTime = time.time()
finishTime = startTime + seconds

# loop until the current system time
# is greater than the finishTime
while time.time() < finishTime:

  # show loop is running
  print "."

  # wait for one second
  time.sleep(1)

print "Time is up!"

Now let's take a walk through each line of code.

Lines 1 and 2 are comments which describe the program. Adding comments is an important part of programming. Programmers spend at least as much time reading code as writing it. Descriptive comments, clear layout and meaningful variable names all help to make the code more readable. Python comments start with a # character and are not executed as part of the program. Feel free to change the wording of any comment to suit yourself.

On line 4 we import a module called time. On line 7 we use a variable to capture the countdown time in seconds. By using the raw_input function we can specify a different value each time the program is run.

Next we set two variables. On line 9 the startTime variable is assigned the current system time, using the module function time.time. Line 10 sets the finishTime, which is simply the startTime plus the number of seconds we input earlier.

Now onto the loop code.

After a couple of comment lines we have a while statement on line 14, complete with conditional test and terminated by a colon : character. As the comment says, this will repeat the indented loop code as long as the current system time (found using the time.time function again) is less than the finishTime.

All we need now is some code inside the loop. The indented line 17 prints a character to the screen, so we have some visual indication that somethings happening. The next indented line calls another time module function, namely time.sleep. This function pauses program execution for the number of seconds specified by the parameter. In this case it's one second.

Python programs run extremely quickly. A loop like this will execute thousands of times a second. So line 17 would also run thousands of times a second. This isn't what we want to happen, so we add this sleep statement on line 20.

And that's the end of our loop. The final print statement isn't indented, so it's only executed when the loop has finished.

Now we can run the timer app.

A post from my Learn Python on the Raspberry Pi tutorial.

No comments: