< Prev  |  Home  |  Next >

Python Variables

The problem with our programs so far is that they only work with the data that we enter ourselves. Like this program, for example:

That’s great for me, if that happens to be my name. But sadly it isn’t. It’d be nice to customise the program to actually say hello to the actual person using the actual program. We can easily do this by using a variable. A variable is like a box that stores a value, which can then be used in our program.  Let’s design how we want our greeting program to work.

I’ve sketched the design in a flow chart. The flow chart is fairly straightforward, and shows that we need to ask the user their name, and then say hello to them personally.

Design of the greeting program

As you can see, certain actions have different symbols. The ‘Start’ and ‘Stop’ parts of the flowchart just tell us where the flowchart begins and ends. These are in ellipses (squashed circles). Getting the user to input their name is in a parallelogram (a squashed rectangle), as is providing output to the user. All other processes are written in rectangles. There aren’t any additional processes in this flowchart. You can just follow the arrows to see how the program should work.

Here’s how we write the program in Python:

The program should hopefully work like this:

What is your name? Saarah
Hello Saarah!

Isn’t that so much better? Now you feel like the program knows your name (even though you just told it). But the computer doesn’t really know who you are. It’s just pretending, like when you bump into someone and pretend to know who they are, when actually you have no idea. Let’s test it!

What is your name? Amaar
Hello Amaar!

What is your name? None of your business
Hello None of your business!

What is your name? , I'm an idiot
Hello , I'm an idiot!

When you’ve finished making fun of a computer then we’ll move on. Notice how some lines in our program start with a ‘#’? Well they are comments. Python ignores any lines starting with a ‘#’, but they are useful to us as programmers.

As an example, have a look at this program (don’t worry about typing it in):

Do you know what this program does? Difficult to understand, isn’t it? Well now read it again, but this time with comments added:

You haven’t learnt enough Python yet to know exactly how this program works, but with the comments you get the general idea. This is useful to other people trying to read your program, but also useful to yourself when you come back to an old program and forget what it was supposed to do. The first comment is maybe taking things too far though.

Also, the variable in our program is called ‘name’ but I could’ve called it anything I wanted. It’s handy to name a variable something that will remind you what it’s supposed to be storing. The lines:

Are valid in Python, but not particularly helpful.


Write a program to display a banner for a user entering their name. So, for example, if the user enters the name ‘Xephos’, this lovely banner could appear:

You should also include a design flowchart, and add comments to your program explaining what each part does.

< Prev  |  Home  |  Next >