Archive for the ‘Raspberry Pi’ Category

Guess the Number


Back in the early days of this blog, I posted a screenshot of a program that plays “guess the number”, but I thought I’d like to do a proper post on it,

This program is very simple, so I won’t write too much about it. The only thing here that might be new is the “try:” section. This is a bit of error handling. If your user types in a number above 100 or below 0, the program can cope very well. But if the user types in something that is not a (whole) number, there would be an error if we didn’t use this approach. Basically, the program tries to convert “guess” to an integer. If this isn’t possible, the “ValueError” is trapped and the code tells the user that he/she didn’t enter a number.

Anyway, have a go. “Guess the Number” is a classic beginner’s program!



Use pygame to make a gui.


Remember that I said the main loop in our tkinter gui program was like a pygame graphics window? Well in this post I am going to use pygame to make a gui.

Here’s the code.

[Note – I will add to this post later!]

Getting started with GUIs


So far all the programs I’ve done on here have been decidedly old-school and run in the command line. As I’ve mentioned before, I am learning as I go and I haven’t got very far yet with using GUIs, but I thought I’d do a post that uses the tkinter features of Python to make GUIs.

Here’s what our first GUI will look like. You can move it, resize it, minimise it and click the button (although it doesn’t do anything!).

Here’s the code.

The first line imports the tkinter modules we need. The next lines create a window and give it a size and a title. You can play around with changing these aspects, if you like. We are using a “class” within tkinter here. We’re getting into Object Oriented Programming with this little script. The line: “root = Tk()” makes a new object: an instance of the class “Tk”. We can then use some of the “methods” which that class contains. The method “title” sets the title and “geometry” defines the size.

The dot notation shows us that we are using methods. We have used this type of thing before, for instance, to raise a number to a power we said something like:

i = math.pow(2, 3)

Here the dot notation shows that we are using one of the methods defined in the math module. This one is called “pow” and it raises the first number to the power of the second. An even better example would be when we sorted a list:


Here we have created a list (“mylist”) and we use its name followed by “.sort()” to get the list-method “sort” to order our list.

Next we need a “frame” within the window, and a button. The “.grid()” method causes the layout manager to make the items we have created visible.

At the end of the code we start the main loop. This makes the window keep displaying itself on the screen and checking for any action the user might make. You may have seen similar things if you’ve had a go at pygame in which our graphics windows are controlled by a loop which runs continually until we quit the game.

I hope this makes sense! Anyway, the code itself is pretty simple. Look out for more GUI posts!




The version of the Java Development Kit you have installed in RacyPy2 is 1.7.0_02.

Firstly, I’d like to recommend Herbert Schildt’s books. I am using “Java: The Complete Reference”.

You can download the (free) first chapter of his “Java: A Beginner’s Guide” from various places online.

I am not going to write a lot about Java here – it’s better if you read the Schildt chapter. But I will mention that in Java, all programs use the Object Orientated programming model. We will get around to using this approach in Python (it’s very useful and amazingly powerful). But for simple programs, we haven’t really needed to use it.

What this means in practice is that every Java program has at least one “class”. Here’s an example:

The first four lines are a multi-line comment – we use /* to start these and */ to close them.

Next we have the command that declares the main “class”  of the program. It’s essential that this has the same name as the name of the file. It’s traditional to give this class an initial capital letter. Java is case-sensitive. So if i have called my file “”, my class has to be called “Hello” (not “hello”). The curly braces “{” are used to group a block of code – here the contents of this class.

The next line is a single-line comment which we do by starting the line with “//”.

The next line is one you will see in almost all Java programs (I think – I am only a learner!). I’m not entirely sure what each of the commands means yet! But the important thing is that this line include the call to “main()” which all Java programs need. The next curly brace opens a new code-block – this time for what our program will actually do. What this line actually does is to invoke a class called “System” which gives us control of things like output to the screen. Within this class there is a method called “out” which does just that – outputs to the screen. The “println()” bit is just like print() in Python3 – it will print what we put inside the brackets – in this case a string enclosed in quotation marks – and add a newline. The line ends with a semi-colon because all “statements” in Java must end with a semi-colon. The lines that don’t end in semi-colons are not actually “statements” (yes – I found this a bit confusing at first, but you’ll get the hang of it!).

Then we close the curly braces that enclose the “main()” part of our code, and finally close the curly braces that end the description of our class “Hello”.

Anyway, the fun bit comes next! We have to compile our program!

Open a terminal in the directory where you saved “”. Then type:


This compiles your program into Java bytecode. You then issue the command:

java Hello

And your program should run.

Just to make this really clear, I’ve done a few extra commands here. Typing “pwd” displays the directory we are in. Then “ls” lists the files in it. As you can see, there’s just the file “Hello,java”. Once we have run “javac” a new file is created: “Hello.class”. When we type: “java Hello”, Java looks for a class called “Hello” and executes it, printing: “Hello World!”.

Wow, are you having fun yet!


C++ (and other things) in RacyPy2


If you want to compile in RacyPy2, you need to load the devx file.

It is on the cd (it’s called “devx_racy_5.2.2.sfs”).

If you want to compile you’ll need to do a frugal install (this is quite simple and if you need some help, go to the Puppy Linux forum, or leave me a comment).

Then you need to put the devx file in your /mnt/home folder.

Then choose Menu > System > BootManager.

Click on the button at the top that mentions sfs files. Your devx file should appear in the list on the left. Click it and add it to the list on the right. Then reboot the machine and you will be able to compile.

There is one little problem with this – when you load the devx it will break the script that launches the Python2 IDLE when you click on this snake on the desktop.

I will sort this out for the next release. In the meantime all you need to do is download this .pet (choose open with PetGet) and it will fix the problem!

Happy compiling!


“Guess my Number” in Scratch


I like Scratch! I was having a play with it yesterday and made this. I had to use nested if … else commands because there doesn’t seem to be away to do something like “elif”. If you know a better way to do it, please tell me!

Extra stuff for RacyPy2



Pets are easily installed packages for Puppy Linux. Just click the link and choose open with PetGet.





I don’t know a huge amount  about Lua and Ruby, but these websites should get you started.




If you are using RacyPy and need some help with general Puppy Linux stuff, the forum here is great.


Get your Raspberry Pi kit together!


Okay, so you have “registered an interest” or pre-ordered or even (lucky few) ordered an RPi. It’s time to get together the stuff you’ll need.

1. A screen. The most obvious choice is a TV with an HDMI input.

2. A usb mouse. If you don’t have a spare around, why not treat yourself to a new one – you’re worth it!

3. A usb keyboard. Again, you may have a spare. I’ve ordered a small one, for portability.

4. An ethernet cable. For the web!

5. An operating system. I’m still not quite sure if the Pi is going to ship with one at this early stage. There are lots of places where you can find instructions for setting up your SD card. So far Debian and Arch are available to download via the RPi site. Which brings me to:

6. An SD card. The OS images are about 2Gb. Some people have found that they don’t fit on a 2GB card. I’d get at least a 4Gb card (and it’s worth getting a high speed one – this may cost almost as much as the RPi itself).

7. A power supply. You know the thing you plug your iPod / Kindle into and then plug into the mains – that’s it. You can buy these quite cheaply if you don’t have one.

8. A cable – standard USB to micro USB (to plug into the Pi).

9. An HDMI cable. The cheap ones work just as well as the expensive ones!

10. If you can’t use ethernet, you might need a wireless dongle in order to get online. This probably means you’ll need a USB hub (there are only two USB ports on the Pi). It’s a good idea to buy a powered one, as the Pi itself won’t be able to power many USB devices.

11. Lego. To make a case for your pi 🙂

I think that’s it. So are you ready?


In the mean time, you can pretend you have an RPI by downloading and using RacyPy2!

Hack your homework: grid multiplication


Hacking your homework is not cheating. To write some code that does what you want, you’ll have to think about the maths (or whatever) very carefully. They say the best way to learn something is to teach it. When you write a program, you are teaching your computer to do something.

This program teaches the computer to use the grid method of multiplication. The key to a good maths homework hack is to get the computer to output the “working out” as well as the answer. Here’s a little screenshot of my program’s output.

As you can see, this program takes two numbers and prints out a little table representing the “grid method” way of doing multiplication and then gives you the answer.

Here’s the first part of the code:

The program starts with the usual comment lines, explaining what we are doing. Then we need to import the “math” module, because we are going to need a new mathematical function: math.pow().

I then define a function. We’ve looked at functions a bit before – by doing it this way, I can separate my numbers into hundreds, tens and units just by calling: split().

I create an empty list: grid, into which I can put the values I create. I also initialize a couple of variables, j  and k. The loop that follows is actually the most fun part of the program. I was wondering how to separate out the hundred, tens, units and so on. This is how I did it. If you can think of a simpler way, please let me know.

The loop basically takes the number and divides it by increasing powers of 10 (ie 10, 100, 1000 etc.). This is why we needed the maths module. To raise a number to a power, we need to use math.pow(). For instance if we want to square 3, we would call math.pow(3,2). Here we are using powers of 10. 10 to the power 1 is 10, squared it’s 100 and so on.

The modulus of each of these divisions gets put in “y” to start with. For instance if your number is 14, the remainder when you divide by 10 is 4 – in other words the unit part of the number. If the result is not zero, we add it to the list (we don’t want to bother with the zeros – if you multiply by 1024, for instance, you can ignore the zero for the purposes of your grid). Next I take the y away from x, because I don’t need to worry about that number any more – it’s safely stored in my list. We add one to k, so that next time through the loop we will be using the next power of 10. Finally we divide x by that next power – if the answer is zero, there’s nothing else to do and the while loop will stop.

At the end of the loop I sort the numbers in descending order and send the list “grid” back to the main part of the program.

Here’s the next part:

That’s the hardest bit done. The main part of the program gets the two numbers, sends them off to split() and stores the lists that are produced.

The next bit prints the top line of our table. I’ve made the column-width 7 – you’ll need to alter this if you are working with bigger numbers.  I use a “for” loop to print all the numbers in the first list. I have used “repr” here. This is a bit like “str” – it converts a number into a human-readable form. It also takes the method “.rjust(x)” which right-justifies the value in a column of width “x”.

The next bit is simple too – there’s a nested loop which goes through all the numbers in both lists, multiplies them and prints them. I used “col” to collect the sum of these numbers for the “totals” column and “ans” to keep a running total of that column.

The final line prints the result.

At the moment this program only deals with integers. If you have to use the grid multiplication method for floating point numbers, you could modify this program to deal with that. (Hint: 10 to the power -1 = 0.1).

happy coding!


While you wait for your Raspberry Pi, why not use RacyPy2?


How to use RacyPy or WaryPy.

1. Download the .iso.

2. Download and install DVD Decrypter (or use you favourite .iso burning software).

3. Right click on the .iso file and choose “Burn with DVD Decrypter.

4. Reboot your PC. It should boot from the CD, if it doesn’t do this by default, look for a message telling you what to press for boot options (often F2 or F9).

5. That’s it.


To run WaryPy in Virtual Box.

1. Download WaryPy.vdi.

2. Download and install VirtualBox.

3. Start VirtualBox. Choose “new”.

4. Follow the instructions on screen and choose “Linux” and Linux 2.6.

5. Choose “use existing hard disk” and find the .vdi file.

6. Start your new virtual machine.


Since Liz posted on the RPi homepage about this LiveCD, I’ve had a lot of hits on the blog. Quite a few people have asked about running Puppy in VirtualBox. This works really nicely and I have uploaded a .vdi file for WaryPy (this seems to be better for VB as it doesn’t need PAE).

The .iso of RacyPy is  here.

This Operating System is aimed at people with no experience of using Linux. If you are excited about the Raspberry Pi and keen to start learning to program, this could be useful to you.


1. The original version only came with Python 3 and Pygame. This one has Python 2.7.2 and 3.1.4. Both have Pygame installed which means you are ready to start learning to code whichever version you favour.

2. On the old version I had added a lot of tutorials for several programming languages. This time I have decided to keep it simple. There are lots of great materials for learning Python (you’ll find them in an archive file in my-documents). I’ve left out the others, becuase I am sure that those who want to try other programming languages will have no difficulty finding them.

3. I have included the Java Development Kit (1.7.2). This means that if you wan to learn Java too, you can compile and run Java code without needing to install anything.

4. Typing tutorial included. The nice “gtypist” program is installed, because good coding requires typing skills!

As before, there’s no need to install anything – you just put the disc in your optical drive and boot from it. You can save your work to a memory stick or onto the hard drive without doing anything at all to your existing operating system. If you want to install it, there’s lots of help on the Puppy forum.

As before, this OS is based on Racy Puppy. Racy has a very up-to-date Linux kernel. But it’s a lightweight distribution that should run nicely on quite old machines (eg it’s fine on my old Toshiba laptop with a Pentium 3 processor and 256mb of RAM). If there’s an old computer in your house that no-one uses any more, you will probably find that RacyPy runs really well on it. So you can use this system while we wait for our RPis to be delivered!

happy coding


BugFix (if you haven’t got the latest version).

You may notice that Java isn’t working in Seamonkey. To fix it, click on this link and choose open with petget. I put the wrong link in the plugins folder – oops!