Let's take the programming knowledge we've gained and create a simple guessing game, where we have to guess a mystery number. Following our structured programming theme most of the code will be contained in a two functions.
Create a new file with Geany and save it as 'guess.py'
in the Desktop's Python folder. Now type in the source code, as below:
1 |
As always we start with our program-level comments. On line 4, we have an import statement to load the 'random'
module. As you might expect from the name, this module can generate random numbers.
Our function definitions come next. It's good practice to put all the functions definitions towards the top of the code. However, this means the main program execution flow continues further down, So, we'll follow the flow and look at the body of these functions shortly.
The execution will continue on line 33, where we ask the user for the player's name using the 'raw_input'
function. The value is stored in the 'player'
variable. Now for the game loop.
The Game Loop
Line 36 starts an endless loop with a 'while True:'
statement. This means we can play the game again and again without having to continually rerun the program (the Ctrl-C
key sequence will exit the game).
Thanks to our functions there are just two indented lines in our game loop, and so is easy to read and understand.
On line 37 we call the 'generateNumber'
function passing in the 'player'
variable and store the return in 'number'
. And finally, we call the 'guessNumber'
function and pass in 'number'
on line 38.
Random Number Generation
Now it's time to look at the first function we defined, 'generateNumber'
, defined on line 7.
Line 8 uses the passed 'player'
parameter to print out a message asking the player to make a guess.
While line 9 calls the random module's function 'randint'
with two parameters representing the lower and upper values of a range, namely 1 and 20. This generated number is returned to the function call statement.
The Guessing Loop
Time to look at our second function definition 'guessNumber'
. This contains most of the program's logic. As you can see it has a single parameter, called 'number'
.
After the function definition statement on line 12, we initialise a 'guessCount'
variable to zero on line 13.
Line 16 starts another 'while'
loop. We are going to give the player up to five guesses. So each time the loop repeats we test the guess count to see if it's less than five.
The first indented line in our loop, line 18, increments the value of the guess count by one.
Next we ask the player for a guess. This is done with the 'raw_input'
function again, but this time it's wrapped inside another function called 'int'
. Note the brackets around the whole 'raw_input'
function call, where one fuction is nested inside another. The 'int'
function ensures we'll have an integer stored in the 'guess'
variable.
All we need to do now is test the player's guess against the 'number'
parameter. Starting on line 21 we use the 'if'
keyword for this. In fact, we'll need three of them. One to test if the guess is correct, and two more to test if the number is higher or lower. The last two tests will enable us to print a helpful message for the player.
You'll notice the last two tests actually use the 'elif'
keyword. In Python this is shorthand for 'else if'
. What's the advantage of using 'elif'
instead of 'if'
? Well, if the previous 'if'
or 'elif'
test is 'True'
, any remaining 'elif'
statements are skipped during execution. This makes our program more efficient, and therefore a little faster to execute - something that can be very important when writing certain types of programs, such as a fast-action game.
Making Changes
That's it, our guessing game is finished. I hope you can see how breaking your program up into well-defined, re-usable functions improves its overall readability and make it easier to understand, debug and maintain.
For example, if you'd like to alter the range of random numbers generated, say from 1 to 30, it's the 'generateNumber'
function that needs to be modified. However, if you wish to decrease the number of allowed guesses from five to four, it's the 'guessNumber'
function that will have to be changed.
During debugging each function can be called and tested one at a time, simply by commenting out the other function calls. In this way the full app is run only after we are sure each function behaves as intended.
A post from my Learn Python on the Raspberry Pi tutorial.