🎯 Syllabus & Goals 3 min
Cambridge 8.1.3 · Input and output 8.1.4 · Sequence Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write
INPUTandOUTPUTstatements with clear prompts and messages, in pseudocode and Python. - Convert keyboard input to the correct data type in Python with
int()andfloat(). - Use a trace table to show how the order of statements changes a program's results, and correct it.
Textbook: Chapter 8, §8.1.3–8.1.4(a) (pp. 304–309)
Recap / Warm-Up 5 min
Last lesson: variables and constants are named stores, and each has a data type — INTEGER, REAL, CHAR, STRING or BOOLEAN.
Quick starter
Which data type would you give to each of these inputs: 12, 12.00, X, X marks the spot, TRUE?
Reveal the answer
INTEGER, REAL, CHAR, STRING, BOOLEAN.
🧠 Key Concept 14 min
1 · Input with a prompt
For IGCSE, programs take input from a keyboard and send output to a screen. A user must know what to type, so every input needs a prompt first.
Cambridge pseudocode
OUTPUT "Enter the radius: " INPUT Radius
The prompt is a separate OUTPUT statement.
The same in Python
radius = float(input("Enter the radius: "))
Python puts the prompt inside input(), and float() converts the text.
2 · Output with a message
A bare number on screen means nothing to a user. Each output should include a message saying what the value is. When one output has several parts, they are separated — by a comma in pseudocode and in Python.
OUTPUT "Volume of the tank is ", Volume
print("Volume of the tank is", volume)
3 · Sequence — the order of the steps
A computer carries out statements one after another, top to bottom. An incorrect order gives wrong results or adds steps that are not needed. Think of a recipe: icing a cake before baking it does not work.
Worked Example 12 min
Example 1 · A complete input–process–output program
A bus company charges $0.25 per kilometre. Write a program that asks for the passenger's name and the distance, then outputs the fare.
- Name the constant and variables, with types.the rate never changes; the name is text; distance and fare have decimals.
- Prompt, then input, each value.the user must know what to type.
- Calculate only after both inputs.sequence — the data must exist before it is used.
- Output with a message.the user must know what the number means.
Cambridge pseudocode
CONSTANT FarePerKm ← 0.25 DECLARE PassengerName : STRING DECLARE Distance : REAL DECLARE Fare : REAL OUTPUT "Enter your name: " INPUT PassengerName OUTPUT "Enter the distance in km: " INPUT Distance Fare ← Distance * FarePerKm OUTPUT "Hello ", PassengerName OUTPUT "Your fare is $", Fare
The same in Python
# Bus fare calculator: input, process, output FARE_PER_KM = 0.25 passenger_name = input("Enter your name: ") distance = float(input("Enter the distance in km: ")) fare = distance * FARE_PER_KM print("Hello", passenger_name) print("Your fare is $", fare)
Output
Enter your name: Alex Enter the distance in km: 12 Hello Alex Your fare is $ 3.0
What happens if you forget to cast? Python refuses to do maths with text:
age = input("Enter your age: ") print(age + 1)
Enter your age: 14 TypeError: can only concatenate str (not "int") to str
Example 2 · Finding and fixing a sequence error
A runner types in the length of each run, then -1 to finish. The program should output the total, the number of runs and the average. Test data: 5, 3, 4, -1.
Version A — the steps are in the wrong order
DECLARE Distance, Total, Average : REAL DECLARE Count : INTEGER Total ← 0 Count ← 0 OUTPUT "Enter each run in km, -1 to finish" REPEAT OUTPUT "Distance: " INPUT Distance Total ← Total + Distance Count ← Count + 1 UNTIL Distance = -1 Average ← Total / Count OUTPUT "Total distance: ", Total OUTPUT "Number of runs: ", Count OUTPUT "Average run: ", Average
| Distance | Total | Count | Average | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | Enter each run in km, -1 to finish | ||
| 5 | 5 | 1 | ||
| 3 | 8 | 2 | ||
| 4 | 12 | 3 | ||
| -1 | 11 | 4 | ||
| 2.75 | Total distance: 11 · Number of runs: 4 · Average run: 2.75 |
All three outputs are wrong: the -1 that only signals "stop" was added to the total and counted as a run.
Version B — the same statements, re-ordered
- Set
Distance ← 0andCount ← -1before the loop.the first pass adds 0 and brings Count to 0, so nothing false is recorded. - Move
INPUT Distanceto the end of the loop body.each value is added on the next pass — after it has been checked byUNTIL. - When
-1is typed, the loop ends before it can be added or counted.the rogue value never reaches Total.
DECLARE Distance, Total, Average : REAL DECLARE Count : INTEGER Distance ← 0 Total ← 0 Count ← -1 OUTPUT "Enter each run in km, -1 to finish" REPEAT Total ← Total + Distance Count ← Count + 1 OUTPUT "Distance: " INPUT Distance UNTIL Distance = -1 Average ← Total / Count OUTPUT "Total distance: ", Total OUTPUT "Number of runs: ", Count OUTPUT "Average run: ", Average
# Correct order: add the PREVIOUS value, then ask for the next one distance = 0 total = 0 count = -1 # starts at -1 so the final -1 is not counted print("Enter each run in km, -1 to finish") while True: # Python has no REPEAT...UNTIL total = total + distance count = count + 1 distance = float(input("Distance: ")) if distance == -1: break average = total / count print("Total distance:", total) print("Number of runs:", count) print("Average run:", average)
Output
Enter each run in km, -1 to finish Distance: 5 Distance: 3 Distance: 4 Distance: -1 Total distance: 12.0 Number of runs: 3 Average run: 4.0
| Distance | Total | Count | Average | OUTPUT |
|---|---|---|---|---|
| 0 | 0 | -1 | Enter each run in km, -1 to finish | |
| 5 | 0 | 0 | ||
| 3 | 5 | 1 | ||
| 4 | 8 | 2 | ||
| -1 | 12 | 3 | ||
| 4 | Total distance: 12 · Number of runs: 3 · Average run: 4 |
Try It Yourself 12 min
Goal: Write pseudocode and Python that ask for a user's first name and favourite number, then output both with a friendly message.
Goal: Write a program that inputs the radius and length of a cylinder and outputs its volume (π × r² × length). Use a constant for π. Test it with radius 2 and length 10.
Goal: These statements convert minutes to hours and minutes, but they are in the wrong order. Put them in the correct sequence and dry-run with 135.
OUTPUT Hours, " h ", Minutes, " min" Minutes ← TotalMinutes - Hours * 60 INPUT TotalMinutes Hours ← DIV(TotalMinutes, 60) OUTPUT "Enter a number of minutes: "
Hint
Ask: what does each line need to exist first? The prompt comes before the input, and Hours must be known before Minutes. DIV gives the whole-number part of a division (you will meet it fully in Lesson 6).
📝 Exam Practice 10 min
State why an input statement should be preceded by a prompt.
Mark scheme
- So the user knows what data they are expected to enter (1).
Describe what is meant by sequence in a program, and why it matters.
Mark scheme
- Sequence is the order in which the statements / steps are executed (1).
- An incorrect order can give incorrect results / use data before it has been input or calculated (1).
Write an algorithm in pseudocode that inputs the price of an item and the number bought, then outputs the total cost with a suitable message.
Mark scheme
- Prompt and input of price (1).
- Prompt and input of quantity (1).
- Total calculated as price × quantity, after both inputs (1).
- Output of total with a message (1).
A student writes the Python statement mark = input("Enter mark: ") and later total = total + mark. Explain why this causes an error and how to correct it.
Mark scheme
input()always returns a string (1)…- …and a string cannot be added to a number / used in a calculation (1).
- Correct by casting:
mark = int(input("Enter mark: "))(1).
🗝️ Recap & Key Terms 3 min
Prompt, then input. Convert text to a number before doing maths. Output each result with a message. Statements run top to bottom, so the sequence must give each step the data it needs.
- Input
- Data entered into a program, e.g. from a keyboard, and stored in a variable.
- Output
- Data or messages sent from a program, e.g. to a screen.
- Prompt
- A message that tells the user what data to enter.
- Casting
- Converting a value from one data type to another, e.g.
int("12"). - Sequence
- The order in which the steps in a program are executed.
Homework 1 min
Task (≤ 15 min): Write a program in pseudocode and Python that inputs a temperature in Celsius and outputs it in Fahrenheit (F = C × 1.8 + 32). Dry-run it with 25 and show the output.
Model answer
DECLARE Celsius, Fahrenheit : REAL OUTPUT "Enter the temperature in Celsius: " INPUT Celsius Fahrenheit ← Celsius * 1.8 + 32 OUTPUT "That is ", Fahrenheit, " Fahrenheit"
celsius = float(input("Enter the temperature in Celsius: ")) fahrenheit = celsius * 1.8 + 32 print("That is", fahrenheit, "Fahrenheit")
Enter the temperature in Celsius: 25 That is 77.0 Fahrenheit