GoalChoose valid identifiers, use assignment and arithmetic operators, and follow an algorithm line by line.
New words
variable · a named place in memory that holds one value, which can changeidentifier · the name of a variableassignment · storing a value in a variable, written with an arrow ←arithmetic operator · a symbol that calculates: +, -, * or /
Explanation
A variable is a named place in memory that stores one value, and its name is called an identifier. An identifier must be one word with no spaces. It must not start with a number or a symbol, and it must not be a reserved word of the language, such as if or class in Python. Keep to one style of capital letters, because score and Score can be different names. Storing a value is called assignment. We write it with an arrow, so total ← 5 + 2 means: work out the right side, then store the answer in the variable on the left. The equals sign = is kept for comparing. The arithmetic operators are +, -, * for multiply and / for divide. To follow an algorithm, read one line at a time and write down the new value of each variable, crossing out the old one.
Operator
Meaning
Example
Result
+
add
7 + 2
9
-
subtract
7 - 2
5
*
multiply
7 * 2
14
/
divide
8 / 2
4
Worked examples
Identifier
Valid?
Reason
total_cost
yes
one word, starts with a letter
2ndGoal
no
starts with a number
final score
no
has a space, so it is two words
price$
no
contains a symbol
class
no
a reserved word in Python
Which of these are valid identifiers? Each bad one breaks a rule.
After line
stamps
bought
cost
1
12
2
12
5
3
17
5
4
17
5
34
5
10
5
34
A trace table records every variable after each line. Follow this algorithm: 1 stamps ← 12, 2 bought ← 5, 3 stamps ← stamps + bought, 4 cost ← stamps * 2, 5 stamps ← stamps - 7. Every row of the table shows the values after that line has run.
Assignment can use input values. The user types 40 and then 6. The price and the tax are added and the result is stored in total.
Class activity
Cups and paper (no computer). Label three paper cups or three squares of paper a, b and c. Write each value on a small slip of paper and put it in the right cup, replacing the old slip when a value changes. Follow these lines in order: a ← 8; b ← a * 3; a ← a - 2; c ← a + b; b ← c / 5. Show your cups to a partner after each line. (Check: at the end a holds 6, b holds 6 and c holds 30.) Then invent five lines of your own for a partner to follow. If you have a computer or tablet, run the same lines in Python to check (Python uses = instead of ← and shows b as 6.0, because / always gives a real number).
Practice
1
Follow the algorithm and copy the trace table into your book. What is shown at the end?
cups: 4, 6. plates: 12, then 6. Line 1 stores 4. Line 2 stores 4 * 3 = 12. Line 3 changes cups to 6. Line 4 changes plates to 12 - 6 = 6. The output is 6.
2
Which of these identifiers are valid? goal_count, 3rd place, Time, class, my_age. Give a reason for each one that is not.
goal_count, Time and my_age are valid. 3rd place breaks two rules: it begins with a digit and contains a space. class is not valid because it is a reserved word in Python.
3
Write pseudocode that asks for the kilos of rice and the price of one kilo, and then shows the cost. What does it show for 3 kilos at a price of 4?
INPUT kilos / INPUT rate / cost ← kilos * rate / OUTPUT "Cost: ", cost. For 3 kilos at 4 it shows Cost: 12.
4
Find the mistake in this algorithm and write the corrected line.
Pseudocode
1INPUT width
2INPUT height
3width * height ← area
4OUTPUT area
Line 3 is written the wrong way round: the variable that receives the answer must be on the left of the arrow. Correct line: area ← width * height.