Structure programs as input, process, output, and plan them in plain language before writing any code.
About 35 minBeginner4 exercisesFree
You will be able to
Identify the input, process and output parts of a small program
Plan a program as comment steps before writing code
Work out a test case by hand and use it to check your program
Put the parts of a program in the order they need to run
You now know enough Python to write genuinely useful little programs: a bill calculator, a unit converter, a fuel cost estimator. The hard part is no longer the syntax. It's deciding what the program should do, step by step, before you type anything. Experienced programmers rarely start by typing code; they start with a plan.
Input, process, output
Almost every small program has the same three parts, in this order:
Input: get the data the program needs (here, with input() and a conversion).
Process: calculate the results from that data.
Output: show the results with print().
Keeping the parts separate, and in this order, makes programs easier to read and to fix. The order matters because Python runs top to bottom: a result can't be printed before it has been calculated.
Plan first, in plain language
Before writing code, answer four questions:
What goes in? Which values does the user type, and are they whole numbers, decimals or text?
What comes out? Write down the exact output lines you want.
How do you get from one to the other? The calculation, in words or maths.
What is one example? Choose sample inputs and work out the correct output by hand. This becomes your first test.
Then write the plan as comments, and fill in the code under each comment.
Here's a worked example. An electricity company charges 0.18 SAR per kWh used plus a fixed 10 SAR monthly fee.
In: the kWh used this month (a decimal is possible).
Out: Energy charge: ... SAR and Total bill: ... SAR, with two decimals.
How: energy charge = kWh × 0.18; total = energy charge + 10.
Example: 350 kWh → energy 350 × 0.18 = 63.00, total 73.00.
The plan as comments, with code under each step
# Input: kWh used this month
kwh =float(input("kWh used: "))# Process: energy charge and total
rate =0.18
fixed_fee =10
energy = kwh * rate
total = energy + fixed_fee
# Output: both amounts with two decimalsprint(f"Energy charge: {energy:.2f} SAR")print(f"Total bill: {total:.2f} SAR")
Input given to the program: "350"
Try it: Check that the output matches the hand calculation (63.00 and 73.00). Then try 0 kWh: what should the bill be?
Notice the details that make this program pleasant to work with:
The rate and fee have names. If the price changes, there's one clearly labelled line to edit.
The comments mirror the plan, so a reader sees the structure at a glance.
The hand-worked example confirms the program is right, not only that it runs. A program that runs without errors can still give the wrong answer.
Trying 0 kWh checks an edge case: the bill should be only the fixed fee, 10.00.
Predict the output
# Input
nights =int(input("Nights: "))
rate =float(input("Rate per night: "))# Process
cost = nights * rate
service = cost *0.1
total = cost + service
# Outputprint(f"Room: {cost:.2f}")print(f"Service: {service:.2f}")print(f"Total: {total:.2f}")
Guided practice · ●●difficulty 2 of 5 · about 5 min
Average of three marks
The starter contains the plan as comments. Fill in the code under each comment. The program asks for three exam marks (whole numbers) and prints their average with one decimal place. For marks 80, 91 and 82:
Average: 84.3
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●●difficulty 3 of 5 · about 8 min
Estimate the fuel cost of a trip
Plan and write a program that estimates the fuel for a car trip. It asks for three values, in this order (all may have decimals):
the trip distance in km,
the car's fuel use in litres per 100 km,
the fuel price per litre in SAR.
It prints the litres needed (one decimal place) and the cost (two decimal places). For 450, 8 and 2.33:
Fuel needed: 36.0 L
Trip cost: 83.88 SAR
Work out the formula for the litres needed before you write any code. Check it with the example: 450 km at 8 L per 100 km.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●difficulty 2 of 5 · about 6 min
Convert minutes to hours and minutes
A timesheet records work in minutes. Write a program that reads a whole number of minutes and prints it as hours and minutes. For 135:
2 h 15 min
Make sure it is right for values below one hour, exact hours and zero, too: 45 gives 0 h 45 min and 60 gives 1 h 0 min.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 5 min
Put the steps in order
A painter's estimate program asks for a wall's length and height in metres and prints its area and the paint cost at 18 SAR per square metre. It stops with:
NameError: name 'area' is not defined
For 4 and 2.5 it should print:
Wall area: 10.00 m2
Paint cost: 180.00 SAR
Fix the program. All the right lines are there.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
Most small programs follow input, process, output, in that order.
Plan first: what goes in, what comes out, how to get from one to the other, and one example worked by hand.
Write the plan as comments and fill in code under each one.
Check results against your hand-worked example and try edge cases such as zero.