Simple Stuff in Small Basic


Hi, I am off school today. Here’s what I want you to do. Look at the Prezi embedded below. You should have done everything up to and including the “Simple Maths Test” program. Work through the other tasks on there.

Also,  go onto Project Euler and register.

We have already solved the first two problems, so you can submit those and move on to the third and fourth. There are some notes on the Prezi to help you.

If you get that done, have a go at the “Guess the Number” game that is described in the post before this one.


Alan Turing Google Doodle


While researching the first computers, you will have come across the name Alan Turing. He was one of the most important computer scientists ever and helped us to win WWII by breaking the Nazis’ Enigma codes. He committed suicide in 1954. This year is the 100th anniversary of his birth and it is being celebrated as “Alan Turing Year”.

On his birthday in July, there was a Google doodle based on his concept of the “Turing machine”. Click on the image and see if you can solve the puzzles! You have to make the number on the tape match the number in the top right of the window. In solving this puzzle, you are basically writing simple computer programs.

If you get that done and would like to find out more about Alan Turing, here are some useful links.

Here’s the wikipedia entry on Turing.
There is also some good information on the Bletchley Park website and the Alan Turing website.


Monday 9th July



Today, I’d like you to do some research into the life of Alan Turing. Last week, in preparation for this, we had a look at the Google Doodle based on a Turing machine and explored some simple cryptography.

A good place to start is the wikipedia entry on Turing.
There is also some good information on the Bletchley Park website and the Alan Turing website.

Have a read and answer the questions on the sheet.



I thought you might like to be able to chat online to each other in this lesson. Have you used swiftirc before? Click the link, pick a nickname and put #9b2 in the “Channel” box. Simples.



Scratch / Bubble Sort

If you complete that, there are a couple of programming exercises you can work on.

The easiest is to learn about Bubble Sort and try out this Scratch version. This will show you one way that computers sort lists of numbers.



Python and Pygame

If you’d prefer to do some Python, here’s a file to download. It’s a very simple starting point for a game. You’ll also need this sound file to be saved in the same folder as the code.

If you look at the code, you’ll see how easy it is to use sound in Python and make keys do things (in this one, we use the arrow keys to move the ball). I’d like you to experiment with modifying this game – for instance you could add some different sounds and make some keys do other things (can you make a key play the sound, for instance).

If you are experimenting with this, you might find the Pygame homepage useful.


Programming Competition
The Raspberry Pi foundation has announced the first of their programming competitions. They especially like games. I wonder if any of you will enter?

happy hacking,





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


Back to Basics


Objective: For students to become more comfortable using IDLE and writing simple programs.

Success Criteria

ALL: Will use the IDLE editor to save and run a simple program.

MOST: Will run at least two programs, with growing confidence in using Python.

SOME: Will make their own modifications to the programs we are working on.

Last week, some of us got a little confused when we were using Python’s IDLE tool, so here’s a quick refresher.

First, from the Start Menu, choose: All Programs > Python > IDLE.

This will open the Python Shell (or Interpreter). This is where we can enter Python commands and have them executed immediately. It’s useful for testing out individual bits of code, but we don’t use it to write programs.

We need to choose: File > New Window, and then we can write a program.

The first thing I’d like you to do is to “save as” and give the file a “.py” extension. This means that the editor will know we are writing a Python program. Our first program today is going to to be another “while” loop. But instead of the infinite one we did before, this one will only loop around the number of times we want.

This is a nice simple one and you can type it in yourself. Hopefully, you recognise the comment lines now (beginning with: “#”). Then we set the variable: “count” to 0 (zero). We need to do this or the “while” command in the next line will give an error (since we’d be asking it to check a variable that didn’t yet exist). The “while” statement will make the following lines run until “count” gets to 10. In the “while” loop, we have a simple “print()” command. The last line of the program adds one to count.

You can get the next program here. Once you have saved in in your documents, you can open it with IDLE.

We start by setting name to: “None”, so that the “while” loop will work. It will keep the program going until we press enter (as we don’t give it a name to work with). Try running the program. Hopefully most of it will make sense to you. Try changing the names and messages and let your friends have a go.

The next program we are going to look at includes a new idea: random numbers. We are going to use them to simulate the rolling of two dice.

You can get the program here. We have to “import” the module “random” so that we can use random numbers. Next we set variables to zero and use a while loop that will keep going until we get a double 6. The program gets its random numbers using two slightly different methods. The first, “random.randint()” gives us a random number between 1 and 6. On the other hand: “randrange()” gives us a random number between zero and 5 (this is because the computer starts counting at zero). That’s why we have to add 1 to get our dice throw. The rest is pretty easy.

Task: Try changing this program so that it does heads and tails instead of dice. It would be nice to have, say, three coins and to stop the program when we get 3 heads.

If you manage that, let’s try something else: a “for” loop. The “for” loop is very useful and it works a little differently to “while”. Here’s a simple program that prints out a multiplication table.

The “for” loop sets “i” to 1 and keeps adding one to it until it gets to 13. Enter this program and try modifying it to make it do something a little different (perhaps add a certain amount instead of multiply).

It doesn’t only work with numbers.

Try entering this and running it. Have a go at playing around and modifying it to do something else!

That’s all I’m going to put on the blog for today. By now you are getting used to writing simple programs. If you get finished, just ask me for something else to work on, or go over to Team Python, for some other blog posts.


This week’s programming lesson (5/3/12)


Last week, many of you wrote your first ever Python programs.  I was impressed by how quickly most of you picked it up. I am going to try to make sure that there are plenty of ideas on here to keep you busy.

As you know, I made this blog especially for you and these lessons we have on a Monday. There is more stuff on Python, Linux and the Raspberry Pi over on Team Python.

Anyway … let’s start with something nice and simple.

I want you to get into good habits when you are writing programs. First, you should use comments. These make your code easier to understand and easier to fix, if something doesn’t work. You should also use some empty lines for the same reason. Another important thing is to use sensible variable names. For instance, I used “add”, “minus” etc for the results of those calculations – when you start writing longer programs you will save yourself a lot of hassle if you use variable names that mean something to you.

Snip: I’ve taken out the section on making a graphical game with pygame. I want to make some improvements, so I’ll repost it separately.

If you get that done, you might want to have a go at this. It’s quite simple, but fun. I won’t explain it much here – we’ll talk about it in class if you get this far.

Hopefully quite a lot of this is looking familiar. There are some new things in this, like random numbers and “sleep” which makes the game more fun!

happy coding


Programming Lesson


In this lesson:

All of you should write at least one computer program that works.

Most will be comfortable with the idea of using variables.

Some will have tried using loops (control statements).

Jesse Eisenberg in “The Social Network”

It is traditional that when you start to learn a programming language, you begin with some code which prints: “Hello World!”

First open the Python IDLE (Start > Programs > Python > IDLE).

This will bring up the Python Interpreter. The prompt looks like this:


The Python Interpreter

Python is an interpreted language (like, for instance, Ruby and Lua). The interpreter is a really useful tool for when you just want to type in a command and see how it works. Type this:

When you press enter, you should be rewarded with the output:

You’ll notice a couple of things here. Firstly, the thing we want to print goes in brackets. This is because “print” is a function and functions take data to work on from inside brackets. Secondly, the words we wanted to print were inside quotations marks (single ones would work too).

Before we move on to actually writing a program, why not have a play with the interpreter. It’s great for simple maths. Try just typing in:(Don’t copy the prompt out again – I just included that so it looks like you will see).

>>> 2+2

If you are trying this out, don’t forget that computers use “*” for multiply and “/” for divide.

Okay, now let’s write a program.

In the menu bar of IDLE, choose: File > New Window. A blank editing window will pop up. First of all, choose: “Save as” and save this in your documents with the name “”. The extension “.py” is used for python code.

Now type this in:

You have now written a program! The first two lines are comments. The “#” symbol tells the computer to ignore that line, so we put in comments that help us to understand the code. This one is pretty simple, but I want you to get into good habits. There’s no real need to leave a blank line after the comments, but spacing your code out nicely makes it easier to read. To run this program, first press Control-S (to save) and then F5.

We could do with making this program a bit more interactive, using a variable. A variable is a memory location to which we can allocate some data for use in the program. Try this:

The “input” function waits for the user to write something and then press enter. Because we typed: “name = input()”, it stores whatever the user types in the variable “name”. Then when it comes to the print command, we can use the variable again and the contents of that variable will be printed out. There are a couple of things to notice about  “print()” here. First, we can print more than one thing. If we want a space in between the items, we can put a comma. If we don’t want a space we can use the plus sign. I did this here because I wanted that full stop to come right after the name – it looks so much better that way.

While we are looking at “print()”, here’s something you can try (no screenshot this time). After you open the speech marks, add this to your code: “\n”. If you did it right, this should make the computer print an empty line before continuing with the rest of what is in the print statement. Technically, this is an “escape” code – the “\” makes the computer do something different with whatever comes next – and “n” tells it to add a “new line”. Try “\t” as well, if you like!

Okay, let’s try one more program. This demonstrates the use of a “while” loop.

The fun bit here is the while statement. This involves a it of logic. The computer carries out the instructions in the indented block after the colon (“:”) “while” 1 is “true”. Now 1 is always “true”, so we have an infinite loop. By the way, some things are false – zero and empty variables for instance. This logic stuff is really useful!

Right, I think that is about all we will have time for. If you get through all this, try writing some other short programs, using input() and print(). If you are feeling confident, you might like to look at some other tutorial posts over on Team Python. This one has some more information about mathematical operators.

Happy Coding,