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.
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.
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.
Pseudocode
Python needs
INPUT hours
hours = int(input("Hours? "))
cost ← hours * 5
cost = hours * 5
IF cost > 30 THEN
if cost > 30:
ENDIF
no line: the indent ends
OUTPUT "Hire cost: ", cost
print("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.
3 pages: 3 * 2 is 6, which is less than 10, so cost becomes 10. Output: Print cost: 10. 5 pages: 5 * 2 is 10, and 10 < 10 is false, so the cost stays 10. Output: Print cost: 10. 8 pages: 16 is not less than 10, so Print cost: 16.
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
temp = int(input("Temperature? "))
if temp > 25:
print("Open the windows")
else:
print("Keep them shut")
Check: the arrow is not needed, THEN becomes a colon, ELSE gets a colon, the bodies are indented, and ENDIF is not written. float() is also fine instead of int().
3
Write this Python program as pseudocode, in the style of this course.
Python
1books = int(input("Books? "))
2if books > 3:
3print("Ask for a bag")
4else:
5print("No bag needed")
INPUT books
IF books > 3 THEN
OUTPUT "Ask for a bag"
ELSE
OUTPUT "No bag needed"
ENDIF
Another clear style is also acceptable, as long as it is consistent.
4
Nodira writes INPUT name. Her friend writes name = INPUT("Name?"). Nodira says her friend’s pseudocode is wrong. Is she right? Explain.
No. Pseudocode has no fixed syntax. Both lines are clear and both store what is typed in name. The only rule is that each person uses one style all the way through an algorithm and the meaning is clear. (Python, by contrast, has strict syntax.)