Welcome to my Practical Python Programming post, hopefully this will be the first of many. If you have been following my ‘An Introduction to Python Programming’ posts series, then you may already know that in this post we are going to be having some fun. We are going to be creating a text based ‘Guess the Number’ game. All of the knowledge needed to create this game is contained in the ‘An Introduction to Python Programming’ posts, so as long as you have read those you should be able to follow this with relatively ease.
If there is anything that is unclear or that you are unsure of, just drop a comment to the post and I’ll try to shed some light. Lets go over what this game will do. The following process is like writing pseudo-code, which basically means to jot down in plain English what the program should do, so here goes.
import the relevant modules create some needed variables start a loop upon the condition that the users entry does not equal the answer ask for answer tell player if the answer is too high or too low print congratulatory message.
That is basically all we need to do. Seems pretty simple so lets get down to business. First we shall import the random module that will be used to select the random number. We also need to print some introductory messages and create some variables.
import random print "Welcome to my Guess the Number game." print "I am thinking of a number between 1 and 100" print "Can you guess what it is?n" answer = random.randrange(101) guess = "" iter = 0
So, there is the beginning of our game. We have the introduction, the selection of the random number and we also declare a variable containing the guess, which we need to do in order to use it as a comparison in our loop. The final line is a variable called iter (iterations) that we will use to tell the user how many guesses they took to get the answer. The loop is the section that follows needs to prompt the user for their guess, then tell the player if their guess is too low or too high. If they guess correctly, the loop will terminate and the subsequent code will be executed.
while guess != answer: iter += 1 guess = int(raw_input("Guess: ")) if guess > answer: print "Too high, guess lower." elif guess < answer: print "Too low, guess higher."
This loop should be fairly self explanatory. It increases the iter variable every iteration, then checks to see if the answer is too low or high, it then informs the user of this. The
int() function that we have wrapped around the
raw_input() function is needed, it converts the string that the user enters into an integer so that a valid comparison can be done. Otherwise your guess would always be too high because a string is > than an integer as far as python is concerned. Because the successful comparison condition of our loop would result in the loop terminating when the user enters the correct number, we do not need to add an extra clause for dealing with this, we can instead deal with it outside of the loop like so.
print "nn==**==CONGRATULATIONS==**==" print "You chose the right number, it took you", iter, "guesses."
There, thats it. Your first python game! Congratulations. This is a basic guess the number game. It has many bugs and issues with it, but it works! In later Practical Python Programming posts we will look at improving this game with error checking, the use of strings and also a high score table along with a menu. But for now this will do! Well done. Below is the complete script for the game.
#! /usr/bin/env python import random print "Welcome to my guess the number game." print "I am thinking of a number between 1 and 100" print "Can you guess what it is?n" answer = random.randrange(101) guess = "" iter = 0 while guess != answer: iter += 1 guess = int(raw_input("Guess: ")) if guess > answer: print "Too high, guess lower." elif guess < answer: print "Too low, guess higher." print "nn==**==CONGRATULATIONS==**==" print "You choose the right number, it took you", iter, "guesses."
Thanks once again for reading, I hope you enjoyed the post and I hope you enjoy your game. If you have had any problems with the code or with understanding anything just let me know and I’ll try and help.