Archive for February, 2012

How to do a very simple menu

26/02/2012

There are often times when you want to use some form of menu. Here’s one way to do it. First, I make “ans” = True so that it will satisfy the “while” condition. Then the rest of the code is inside the “while” loop. This means that when the user presses enter, “ans” will be false / empty and the program will exit.  Everything else here should be familiar to you, but you may not have seen the use of triple quotations before. We use this in Python to allow us to do multi-line printing. Line returns are allowed so it’s great for this sort of thing and, of course, ASCII art.

Happy Coding,

antiloquax

Hack your homework

26/02/2012

Here’s a program that actually does something useful!

When I was learning to program,  I used to enjoy writing code to help me with my homework. This is a program that allows you to input a list of numbers and then shows you the mean, median and mode.

I am publishing this now as it has been hanging around in draft form for ages. I will come back and comment on it in more detail at a later point. Hopefully the comments make it fairly easy to follow. The main thing I was pleased with was how this program shows the use of lists and tuples.

antiloquax

Using turtle graphics

21/02/2012

In my post on functions, I used the the example of a little program that draws a square. I didn’t say anything about the “turtle” command. It’s pretty obvious what happens though. When you use this command, Python opens a graphic window and draws things. The idea of turtle graphics began in the Logo programming language in the 1960s. The idea was that there was a virtual “turtle” which you could control with simple commands. If you have used “Scratch”, you have been using a version of Logo. I am going to use this post to review a few of the basics.

Here’s a little Scratch program that draws a square:

It’s not a very good program though. There’s no need for it to be that long, or to repeat itself so much. We can make it more elegant by using a loop.

We’ve done the same thing with 4 lines of code, instead of 9. Another advantage is that if we want to change something (the size of the square for instance), we only need to make the change once. Also, cutting down on repetition of lines of code makes it less likely that we’ll type something in wrong, and easier to put right if we do. Smart programmers are lazy – but in a good way. They write programs that are as simple and elegant as possible.

Okay, this program works, but it’s a bit dull. It just does the same thing every time we run it. But we can change that by using a variable. Let’s switch to Python now.

This is basically the same program I used in the post on functions, but now I have added the use of a variable, so that the program will draw a square the size that the user wants.

But what if I want a triangle or some other shape? Well, we can re-write the program quite simply to make it even more flexible.

Now we have a nice simple program that can draw a very wide range of different shapes. Which is nice 😉

Happy Coding

antiloquax

Functions

21/02/2012

What is a function? Well, when we tell the Python interpreter:

>>>print(“Hello”)

we are using the function: print().

A function is something the computer knows how to do. We use functions all the time when we are programming.

It’s useful to create our own functions. If there is a specific thing that we need to do quite often in a program, we can define a function that does it. There are several advantages to this. Firstly, we don’t have to type in the code for this operation every time we need to do something. Secondly, if we need to change something about that action, we only need to change it once, in the definition of the function. Here’s an example.

This function just draws a little square. In a program this simple, there isn’t really an advantage in using a function, but as your programs become more complex, you’ll find them very useful. As you can see, we define the function at the start of the of the program. That way, when the computer comes across our call to that funtion, it will know what to do. To define a function we type: “def function_name():” After this comes an indented block of code which is what the computer will do when we call the function. We call the function by entering: “function_name()”.

Just like  with the other functions we have used, we can use the bracketed part of the function call to pass information to the function. Here’s an example:

This time the function is expecting a value – “side”. So if we say “square(50)”, it will draw a square with ides 50 steps long. If we change the “main” part of the program to:

size = int(input(“How long do you want the side to be?”))

square(size)

the program will ask tell the function to draw a square with lines the length that we specified.

There’s a lot more we can do with function, but hopefully this gives you the basic idea.

Happy Coding

antiloquax

Introducing RacyPy

16/02/2012

I am very proud to announce the birth of “RacyPy”. This is a bootable Linux Operating System with Python 3.1.4 and Pygame built in (it’s based on Racy Puppy).

You can download the .iso from here. Edit – better to get the new version – here.

Once you’ve done that, you’ll need to burn the .iso to a cd, using your favourite burning utility.

Then, you can reboot the computer. Many PCs will automatically check for a bootable CD before looking at the hard-drive, but you might have to hit a key to make the computer boot from the CD.

RacyPy will run in RAM and doesn’t do anything to your hard-drive, unless you choose to install it. When you shutdown for the first time, it will ask you to create a save file. You can have this on the hard-drive or somewhere else (eg a memory stick – you’ll need about 512Mb of space).

When you are running RacyPy, you will notice a little snake icon on the desktop. Clicking this will launch the Python IDLE. If you look in “my-documents” you’ll find tutorials for Python and other programming languages.

I hope you will give it a try – it will be a great introduction to working in Linux, as well as helping you to start coding as soon as possible.

The iso is here.

There are additional packages and some discussion over on the Murga Linux forum.

Happy Coding!

antiloquax

Scratch

15/02/2012

If you are new to programming, you will probably enjoy having  go with Scratch.

Scratch is a programming language, but you don’t actually need to write the code. You create a script by arranging a series of commands (you just drag and drop the instructions into a window and arrange them how you like). Then you can run the script and watch the output.

The emphasis is on graphics, sound and cute characters.

This little program gets the cat to draw an octagon and then miaow!

The best thing is to download it and have a play.

It’s here.

Data Types

15/02/2012

One of the things that can make your programs fail to work as expected is the whole “data types” issue.

The computer stores information in different ways depending on what type of data it is. Basically there are three types that we need to be aware of.

Strings. The computer stores these as a “string” of characters. Strings can contain letters, numbers, pretty much any sort of character. The thing is, if you try to do actual maths with a string, even if it just contains a number, the computer will complain.

Integers. This is what we call whole numbers. The computer wants to be as efficient as possible. If we are only going to be using whole numbers, this data type is fine.

Floating Point Numbers (or “floats”). If we are going to need to work with numbers that have decimals, we need to use “floating point” variables

This little program shows what happens if you get the data types confused.

If you enter this program and run it, you’ll see what is happening. When it is treating the variables as strings, adding them together just prints them out one after another (we call this  “concatenating” strings). When I use the format “int(num_1)”, it tells the computer to convert the string into an integer and then do the maths. If you give the computer numbers with decimals, you’ll see an error when it tries to add them.

To avoid this, we can tell the computer to treat them as floats.

Here, “float(num_1)” tells the computer to treat the contents of num_1 as a floating point number. Of course, if we didn’t enter a number when we were asked, we’ll get an error!

The computer tries to convert the string of letters into a number, but of course it can’t, so there’s an error.

Just for the sake of it, here’s what happens if we enter integers, but get the computer to treat them as floats (don’t worry, this won’t cause an error).

The computer is quite happy to convert these numbers into floats, the answer is “98.0” which shows that we are working with floating point numbers.

Before we leave this topic, I will show you what happens if you use multiplication on strings. (You won’t be surprised to hear that you can’t divide or subtract strings!).

As a wise person once said, a computer does what you tell it to do, not what you want it to do!

Happy Coding,

antiloquax