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

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):

# A timer countdown program
# Created by David Briddock - July 2012
# Updated for Python 3 September 2017

import time

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

count = 0

# loop that counts in seconds
while count > seconds:

  # show loop is running

  # wait for one second

  # increment the counter
  count = count + 1

print("Time is up!")

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

The first few lines 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 5 we import a module called time.

On line 8 we use the seconds variable to capture a user-entered countdown time (note it's converted to an integer). You can choose a different value every time the program is run.

Next we setup a counter variable, on line 10, with an initial value of 0.

Now onto the loop code.

After a comment line we have a while statement on line 13, 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 count is less than the seconds value.

All we need now is some code inside the loop. The indented line 16 prints a character to the screen, so we have some visual indication that something is happening.

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

Now we just increment the counter by one on line 22.

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: