The personal wiki of ...

Exercise: Making Decisions

Programs often require you to get input, make decision based on the input and do one or more things based upon the decision. This exercise covers how to make simple decisions and also repeat commands.

The If ... Then Statement

Consider a situation where you want a user to guess a number that only the programmer only knows. We will want to ask the user for their guess and compare it to the secret number and tell the user if they were correct or not. For this we use a statement called "if", it makes a comparison which is either TRUE (the comparison is correct) or FALSE (the comparison is not correct).

This script asks the user for their guess. Compares it to the password that is held in a variable called secret and tells the user if they are correct or not.

secret = "rubbish"
guess = raw_input("What is the guess?")

if guess == secret :
    print "You got it right."
    print "Try again."

The key part here is the ==, this is a comparison to see if the left hand side of the comparison is equal to the right hand side.

This can be useful if you want to present the user with a choice and do different things based upon the choice.

Repeating the Question

We should give our user multiple goes at guessing number. To do this we add a "while" loop. Anything under the while loop that is indented by four spaces will be repeated until the while is exited. Try out the example below (note that while True means repeat forever).

secret = "rubbish"

while True :
   guess = raw_input("What is the guess?")
   if guess == secret :
      print "You got it right."
      print "Try again."

Can you work out what break might do?

Picking a Random Secret

The only problem with this game is that it always picks the same secret word. We should use the random library to choose a random word.

Add "import random" to the top of your program and change the line that sets up the secret so that it looks like this:

import random
print 'time to play hangman'
secret = random.choice(['tiger', 'panda', 'mouse'])

The square brackets [ ] and commas make a list, and the random.choice picks one thing randomly from the list.

Of course, you can make the list as long as you like. Here is a longer list:

secret = random.choice([

You can write a long list on lots of lines like this, as long as you remember to end any parentheses () and brackets [] that you started. Don't forget to put a comma after each thing in the list. Try this now.

Loading a List from the Internet

There is an even longer list of animals on the internet, at

As long as you are connected to the Internet, you can load this data in python using a function called "urllib.urlopen". (URLs are what web addresses are called, so urllib stands for "URL library," and urlopen means "open a URL.")

The code looks like this:

import urllib
animals = urllib.urlopen('').read().split()
secret = random.choice(animals)

What this means is:

import urllib

Import the library called "urllib" to use.

animals = urllib.urlopen('').read().split()

Open up the address

animals = urllib.urlopen(''). read() .split()

Read the whole webpage into a big string.

animals = urllib.urlopen('').read(). split()

Split the string into a list of words.

animals = urllib.urlopen('').read().split()

Call the whole list "animals".

secret = random.choice(animals)

Choose a random one and call it "secret."

You can use urllib to load up more data than you could ever type in yourself.

Extending the Guessing Game Program

At the moment the program continues looping until you guess the right word. You should allow the user to exit early although have the program tell the user that they have LOST!.

You could do this by exiting the program when the user enters EXIT when prompted for a guess.

Hint. You can have more than one IF statement within a program.
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 13 Dec 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated