Friday, December 7, 2012

Hello Crispin!

In the last post we got the computer to say hello to the world. Now we want to get a bit more personal. Before we start, let's introduce a name for those three little arrows you see on the screen. They are called a command prompt or more often just a prompt. That is because the computer is 'prompting' you to type something in for it to do. It's also a lot easier than saying 'three little arrows' every time. So, at the prompt, type the following:

>>> name = 'Crispin'

Now hit enter and type:

>>> print 'Hello ' + name

Hit enter again, and see what you get. If all goes well (and you typed carefully) you should see:

Hello Crispin

Of course, if you were particularly clever, you may have put your own name in, in which case you would see something else (unless your name is Crispin)

Now, a little secret: programmers are lazy. If there's an easy way to do something, you can be pretty sure a programmer will make it happen. On your keyboard, hit the 'up' arrow once. You should see the 'print' command you typed in appear again. Hit enter. Hey presto, we got the computer to do talk to us with two key strokes. Now, do the following:
  1. Hit the 'up' arrow twice. You should now see the name = 'Crispin' line again.
  2. If you now hit the backspace key (normally next to the key with a + and = on it) you will see it starts to delete what you wrote.
  3. Do this again until you have removed your name
  4. Now type in your friend's name (don't forget the quotes) and hit enter.
  5. Now hit 'up' again until you see the print 'Hello World' + name and hit enter again.
If all is well you should see the computer say hello to your friend!

You have just created and used your first variable. When you typed name = 'Crispin' you created a variable called 'name'. You then assigned the value 'Crispin' to that variable.

If that seems a bit tricky, think of a variable as an empty cardboard box. Imagine writing 'name' on that box. Now write your name on a piece of paper, and stick it in the box. That's what you did with name = 'Crispin'. When you then wrote print 'Hello ' + name you said to the computer, "print 'Hello ' and add the contents of the box labelled 'name' to it".

When you changed the name variable to your friend's name, you were taking out the piece of paper with your name on it, and putting your friend's name in instead.

Hello World!

The first thing we write when we are learning a new programming language is normally a 'Hello World' programme. This is nice, easy (and short!) and gives us a quick way to see how the language works. All it does is print 'Hello World' onto the screen. Let's try. If you are using a Raspberry Pi, double click on the icon called 'IDLE' on the screen. This should bring up a window called 'Python Shell'. Click next to the three little arrows ('>>>') and type the following, exactly as it is written (don't type the three arrows again though!):

>>> print 'Hello world'

Hit the Enter key (also sometimes called the Return key) and see what happens. You should see python say:

Hello world

at you. If it didn't work, python may say something strange, such as:

Syntax Error: Invalid Syntax

This is python's way of saying it thinks you made a spelling mistake, or didn't type something quite right. If this happened, look very carefully at what you typed. Did you remember to put quotes (the little ' marks) round 'hello world'?

Another thing python might say is:

File "", line 1 print 'Hello world' ^ IndentationError: unexpected indent

If you see this, check you didn't put an extra space in before typing.

Python (and lots of other programming languages) may be very good at doing things, but they have to be told exactly what to do. If you spell a command (like 'print') wrongly, Python won't be able to guess what you meant.

Let's see what you actually told the computer to do. First you told the computer that you wanted it to print something onto the screen, then you told it what you wanted to print ('Hello World'). If you think about when you read a book, you know when someone starts saying something, and when they stop by using quotes or speech marks. Python and other programming languages do the same.

So we got the computer to talk to us, which is nice, but wouldn't be nice to make it a bit more personal? That's what we'll look at in the next episode