Variables: boxes with a name
A variable is a box with a name in which a program keeps a value, like your score in a game. That value can change.
Why
How does a game know how many points you have, even after a hundred jumps and ten coins?
The game has to remember your score somewhere. Every time you grab a coin, it looks at the old score, adds something and remembers the new one.
That place to remember something is called a variable. You can think of it as a box with a label: the name is on the label, the value is in the box.
See it
Choose a program and run it line by line. Watch which box changes and what goes into it.
- still emptyscore
- still emptyiloop counter
- set score to 0
- repeat 3 times
- set score to score + 5
- show score
score = 0for i in range(3):score = score + 5print(score)
Description of the picture
Next to a short program are boxes with a name, such as score, a, b, temp, x and the counter i. At every step the line that is running lights up, and the box that changes is marked and shows its new value. At the bottom is what the program shows.
Get it
With score = 0 you make a box called score and put 0 in it. The sign = does not mean is equal to here, but: put in the box.
With score = score + 5 the computer first looks to the right: what is in score now? It adds 5 to that. It puts the result back in the same box. The old value is then gone.
A box always holds just one value. If you want to swap the contents of two boxes, you need a helper box, otherwise you lose one value.
A loop has a variable of its own too: the counter. In for i in range(4): i takes the values 0, 1, 2 and 3 one after the other. You can use i in your calculation.
A good name helps: score or coins says more than x. The computer does not care, but the people who read your code do.
naam = waarde: work out the right side with the values as they are now, put the result in the box on the left. The old contents disappear.
Worked example
Which number does this program show?
The program:
score = 2 for i in range(3): score = score + 4 print(score)Line 1: score becomes 2.
The loop runs 3 times and adds 4 each time: 6, then 10, then 14.
Line 4 shows the contents of score.
- Answer
The program shows 14.
Do it
Choose a level. You get a new exercise every time, checked straight away.
Pitfalls
Wrong:
score = score + 5is impossible, because a number is never 5 more than itself.In code,
=does not mean is equal to, but: put in the box. First the right side is worked out, then the box is filled.Right: Read
=as becomes: score becomes score plus 5.Wrong: After
a = bandb = a, a and b have been swapped.After
a = bthe old value of a is nowhere any more. After that, b simply gets its own value back.Right: Keep a copy first:
temp = a, thena = b, thenb = temp.Wrong: A variable remembers all its earlier values.
A box holds one value. Whoever puts something new in the box throws the old one away.
Right: If you want to keep earlier values, you need more boxes (or later on: a list).
In real life
- Your mobile phone keeps the percentage of your battery in a variable that keeps getting smaller.
- A step counter adds 1 at every step: steps becomes steps plus 1.
- A till in the supermarket keeps the total and adds the price of every product.
- In a game your lives, your coins and your level each sit in a variable of their own.
History
The code in Lus looks like Python, a programming language invented by the Dutchman Guido van Rossum. The first version appeared in 1991. Python is now used in many schools and many companies.
At the table
Together, think up a game with three variables, for example points, lives and time. When does each variable go up or down?
A question to talk about together, for parents and children. There is no wrong answer.