AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Words before code

Week 1 · 1.1 Pseudocode · 3 lessons
1

Pseudocode meets Python

Computing 9: Topic 1.1, pp. 11–24
GoalRead pseudocode and its Python version side by side and say what is the same and what is different.
New words
pseudocode · a way of planning an algorithm in code-like steps; it is not a real language and no computer runs itsyntax · the exact words, symbols and order that a programming language demandsassignment · storing a value in a variable: ← in our pseudocode, = in Pythonstatement · one instruction in an algorithm, usually one line
Explanation

Designers plan an algorithm in pseudocode before they write real code, as they might with a flowchart. Pseudocode is not a real language, so nobody has fixed its syntax, and different books write the same step in different ways. Assignment might be ←, or =. Input might be INPUT cost or cost = INPUT("Cost?"). Python is strict: a missing colon stops the program. Even so, pseudocode is laid out like real code: lines that store data, command words for input and output, and choices made with IF, ELSEIF and ELSE. An IF can carry on with as many ELSEIF tests as it needs, and it may end with a single ELSE for every other case. Whatever style you choose, keep to it and make the meaning clear.

IdeaPseudocode (this course)Another valid stylePython
store a valuefee ← 4fee = 4fee = 4
inputINPUT kiloskilos = INPUT("Kilos?")kilos = float(input("Kilos? "))
outputOUTPUT "Fee: ", feePRINT "Fee: " & feeprint("Fee:", fee)
selectionIF kilos > 5 THEN … ENDIFIF kilos > 5 THEN … END IFif kilos > 5: (indented lines)
Worked examples
Pseudocode
1INPUT hours
2cost ← hours * 5
3IF cost > 30 THEN
4    cost ← 30
5ENDIF
6OUTPUT "Hire cost: ", cost
A bike hire shop charges 5 for each hour, but never more than 30 for a whole day. Here is the plan as pseudocode. With 4 hours typed the output is Hire cost: 20. With 9 hours it is Hire cost: 30, because 45 is more than 30 and is replaced by 30. With 6 hours the cost is exactly 30 and the IF is false, but the answer is still 30.
Python
1hours = int(input("Hours? "))
2cost = hours * 5
3if cost > 30:
4    cost = 30
5print("Hire cost:", cost)
Output
Hire cost: 30
Here is the same algorithm in Python. With 9 typed it prints the line shown. The lines are in the same order. The differences are small but they matter: the arrow becomes =, THEN becomes a colon, the indent replaces ENDIF, and the typed text is converted with int() because input() always gives text.
PseudocodePython needs
INPUT hourshours = int(input("Hours? "))
cost ← hours * 5cost = hours * 5
IF cost > 30 THENif cost > 30:
ENDIFno line: the indent ends
OUTPUT "Hire cost: ", costprint("Hire cost:", cost)
A checklist for turning pseudocode into Python. Go line by line and change one thing at a time. Always check your Python by running it, or by tracing it with a number.
Class activity

Match the lines (no computer). Write six lines of a short algorithm on separate slips of paper in your pseudocode, for example INPUT cups, then IF cups > 3 THEN, then OUTPUT "Big order". On six other slips write the matching Python lines. Mix the slips, then race a partner to pair them up and put them in order. Next, change one slip into a different valid pseudocode style, for example cups = INPUT("Cups?"), and see whether your partner can still read it. If you have a computer or tablet, ask an adult to help you type the Python lines and test them with the numbers 2, 3 and 4.

Practice
1
This pseudocode adds a minimum charge. The user types 3, then 5, then 8 pages. What is output each time? Show how you worked it out.
Pseudocode
1INPUT pages
2cost ← pages * 2
3IF cost < 10 THEN
4    cost ← 10
5ENDIF
6OUTPUT "Print cost: ", cost
2
Write this pseudocode as a Python program. The user types a whole number for the temperature.
Pseudocode
1INPUT temp
2IF temp > 25 THEN
3    OUTPUT "Open the windows"
4ELSE
5    OUTPUT "Keep them shut"
6ENDIF
3
Write this Python program as pseudocode, in the style of this course.
Python
1books = int(input("Books? "))
2if books > 3:
3    print("Ask for a bag")
4else:
5    print("No bag needed")
4
Nodira writes INPUT name. Her friend writes name = INPUT("Name?"). Nodira says her friend’s pseudocode is wrong. Is she right? Explain.