Code

Practical Python Programming

0

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.

Pseudo-code

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.

First section

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.

Inner loop

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.

Congratulatory messages

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.

Complete Script

#! /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.

Craig Soutar
My name is Craig Soutar. I am a computer enthusiast from the UK. I first started using Ubuntu in December of 2007, this was also my first Linux OS. I have since taught myself; with the help of the community documentation, IRC and forums, how to cope with everyday life on Ubuntu.
You may also like
better-wordpress-developer
5 Tips to Being a Better WordPress Developer
WordPress-Freelance-Project
New WordPress Freelance Project: JDMNagoyaAutoParts

Leave Your Comment

Your Comment*

Your Name*
Your Webpage