Two “Games”

I am hoping that you are getting used to using IDLE to write, save and run programs. If you are enjoying these lessons, why not install Python on your computer at home? There are some instructions on the python website (just click on the icon to my left).

Alternatively, you might want to try a Linux environment. The easiest way to get started is RacyPy – it comes with Python and pygame ready-installed. If you need any help with this, just ask me. This is perfect if you are thinking of getting a Raspberry Pi and want to learn how to use Linux while you learn Python.

I thought we’d have a go at two simple games this week. The first is the classic “Guess the Number” thing.

Findin Python a bit hard? Why not try the Scratch version over on TeamPython!

Because I am so good to you, you can download the file here.

The start should look familiar to you. Some comment lines and then we import random so that we can generate a random number.

The print lines should also be no surprise to you. There are a couple of new things though: when we put “\t” into a string we are printing, Python adds a tab space. “\n” makes it give us a new line.

We then use the “randint” method (which we used last week if you had a go at the dice program). In a classic example of sensible variable-naming, this goes into “the_number”. We set guess to “True” so that we don’t get an error when we get to the while loop.

The while loop that follows will keep going until we guess the number (!= means not equal to). Every time we make a guess the computer tells us if we need to go lower or higher.

When we get it right, we exit the loop and get congratulated!

Have a go and see how it works. However this program is not perfect by any means. For instance, if we guess a number that’s over 100 or less that 1, the program just carries on telling us lower or higher. Do you think you could write some code that prints a sarcastic message if we make such a silly mistake?

And what if we enter something that isn’t a number? Try it and you’ll notice that the program crashes with an error. The problem is that we are trying to convert that answer into a number, so we get an error if the user types something silly. It’s a bit harder to stop this kind of error, but ask me in the lesson if you want to know how.

Anyway … have a play with this program and see if you can improve it at all.

The next program is a version of the same game, but this time, the computer has to guess our number.

I’ll let you type this one in :D.

It starts with some instructions. Then we import the random modules again (we’re going to make the computer’s guesses have some randomness to make it more fun). We set “ans” to the empty string(“”), “tries” to zero and make a guess that’s a random number between 1 and 100.

The variables: “high” and “low” are going to store the upper and lower limits of what the number could be, so they start off at 100 and 1.

We have a while loop that keeps going until the computer gets it right (and we enter a”y”). The next lines might look a bit odd. The reason for this is that the input command only likes to print strings, so we need to make a string that has the computer’s guess in it before we use “input”. We add one to tries to keep track of how many guesses the computer takes.

If the computer isn’t right, and we tell it to pick a lower number, it sets the new “high” to one less than the current guess. If it was too low, we make the new “low” one more than the guess. The computer’s next guess is a random number between “low” and “high”, which makes it look more like a guess!

Have a go and see if you can break this program! This time, if we enter something other than “h”, “l”, or “y”, we don’t get an error, but it does mess things up a bit! Can you work out how to fix it?

And another thing – the start is a bit … sudden. It would be nice to have a bit of time to think of a number. Do you think you could add one of the pauses that we used in the “Dragons’ Cave” program?

Here’s the fix for the bogus answers. Add these lines right after: ans = input(write):

if ans not in (“y”, “l”, “h”):

Now, if we type something silly, the computer says “Huh?” and asks us about the same number again. This sort of thing is called “error trapping” and it’s important to do it if you don’t want your program to go wrong. In fact an important part of testing a program is to throw some silly stuff at it and see what happens. If it “breaks” then you need to do some error-trapping.

Here’s how I fixed the integer problem in the first guess program:

As you can see, there’s a command “try:” that we can use, which tests to see if a particular operation is going to give an error. This stops the program crashing!

happy coding



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: