In this sheet, you get to write a simple guessing game: the computer — or, another player — picks a number, and you have to guess it. You get told after each guess whether the guess was too high or too low.
To do this sheet, you need to know:
A typical game might go something like this:
OK, I've thought of a number between 1 and 1000.
Make a guess: 200
That's too low.
Make a guess: 500
That's too high.
Make a guess: 345
That's too high.
Make a guess: 300
That was my number. Well done!
You took 4 guesses.
Would you like another game? no
OK. Bye!
So, the program needs to do the following things.
The first thing on that list, you already know how to do from Sheet 2.
Another thing that you should already know how to do is asking the player for a number and saying whether their number is too high, too low, or just right. You’ve already done something very like this for Sheet 2. You’ll need to use if.
In Sheet 2, there was a section called Over and over and over again , about for loops . Those are very useful when you know in advance how many times you want to do something, but much less useful when you don’t know at the start when you’ll want to end.
That’s the problem here: you don’t know how many guesses the player will make before guessing the right answer.
For this sort of problem, Python has another kind of loop. It’s called a while loop , because (1) it begins with the word while and (2) it tells the computer to do so-and-so while such-and-such is true .
Here’s a simple example. Try it.
x = 1
while x < 100:
print 'x is', x
x = x + 6
You can probably guess what this will do. What do you think will happen if you change the first line to x=101 , so that the condition tested by the while is untrue right at the start? Make a guess, and then try doing it. Was your guess right?
So, now you know how to do something over and over using a while loop. How can you use this to make your program keep asking for guesses until the player guesses correctly?
You might at first think it should go something like this:
blah blah blah *Choose a number*
while guess != number:
blah blah *Ask for a guess*
blah blah *Say whether it was right or wrong*
If you try writing your program this way and running it, you’ll see that there’s a problem. The variable guess (as we’ve called it above), that’s supposed to be a name for the last number the player guessed, hasn’t been set up when the machine first looks at the while. It only gets a value later on.
There are a couple of different ways to get around this. We think the simplest is as follows.
Make the loop say, not while guess != number: or whatever, but simply while True:. A loop that begins while True: will go on for ever — it will never stop.
That doesn’t seem like much of an improvement. But, try the following.
while True:
x = random_between(1, 10)
print x
if x == 5:
break
print 'x was not 5!'
The only new thing here is the second-last line: break. If you try running that program you should be able to guess what break means.
In case you can’t, we’ll tell you: it means stop going round the loop you’re in the middle of, right now.
Now that you know about while and break, you should be able to make your guessing-game program keep asking for guesses until the player enters the correct number. Just put the guess-asking bit inside a while True: loop, and do a break if the player enters the right number.
Keeping track of the number of guesses the player has made is easy. It’s just like keeping track of the number of correct answers given in the tables-testing program in Sheet 2. Have a variable for it, set it to 0 at the start, and add 1 to it each time the player makes a guess.
Now, this game is so exciting that when you’ve played it once you’ll be desperate to play again (maybe). So when the game is over the machine ought to ask you if you’d like another.
You already know about the function read_number(), which lets the user enter a number. But what you want here isn’t a number, but a yes- or-no answer to a question. To get that, use the function read_yesorno(). Try this:
>>> print read_yesorno('Would you like another game? ')
Would you like another game? yes
True
>>> print read_yesorno('Would you like another game? ')
Would you like another game? no
False
If it’s not obvious yet what read_yesorno does, play with it some more: try giving it different questions to ask, and answering differently. What happens if you just say y instead of yes? Does it matter whether you say no or NO? I’ve said this before, but it’s worth saying again: experiment.
Of course, as well as printing the result of read_yesorno you could give it a name
bored = read_yesorno('Are you bored yet? ')
or use it in something more interesting:
if read_yesorno('Would you like some chocolate? '):
print 'Sorry, I have none.'
else:
print 'Very strange.'
What you want to do is to keep on playing games, until the player says that they don’t want another. You can do this by putting the whole program – everything you’ve written so far – inside another while True:, and doing a break if the player says no to “Would you like another game?” .
There are plenty of ways to make this program better. Here are a few suggestions.
When you’ve done that, you could go on to Sheet 5: The Robots are Coming, in which you’ll write a more interesting game using the graphics stuff you learned about in Sheet 3.