Algorithms
Before you design an algorithm, identify the problem's inputs, processes and outputs. This makes clear what data enters the program, what work is done with it, and what results are produced, so the algorithm you design later has a job for every piece of data.
A car park ticket machine asks how many hours you want to park for and accepts coins. Breaking the problem down gives you:
| Part | What it covers |
|---|---|
| Inputs | the number of hours parking is needed, and each coin inserted |
| Processes | adding the coins to find the amount paid, multiplying the hours by the fixed hourly rate to find the cost, comparing the amount paid with the cost, subtracting the cost from the amount paid to find the change |
| Outputs | the printed ticket showing the hours paid for, the change returned, a message on the display |
Once the three lists are settled, the design is mostly about order: get the inputs, carry out the processes in a sensible sequence, then produce the outputs. The order is forced by the data, because the cost cannot be compared with the amount paid until the coins have been totalled.
An input is data that enters a program or system so that it can be used. Without inputs, a program could only work with values it already holds, so it could never react to a new customer, a new reading or a new choice.
In all three cases the data comes from outside the program, which is exactly what makes it an input. Data does not have to come from a person: a reading taken by a sensor is just as much an input as a barcode scanned at a till.
Data that the program has already worked out and stored in a variable is not an input, because it came from inside the program. An input is the new data handed in from outside. When a user types a value in reply to input(), that typed value is the input; the variable it is stored in is only a name the program uses afterwards.
A garden watering system needs three pieces of data before it can choose how long to water for.
| Input | Where it comes from |
|---|---|
| The moisture level of the soil, as a percentage | A soil moisture sensor |
| The maximum run time allowed, in minutes | A settings file read by the program |
| The plant type chosen by the gardener | The user |
Each of these comes from outside: the moisture level was measured rather than calculated, the maximum run time was loaded from a file, and the plant type was chosen by a person. Naming the actual data item, such as the soil moisture percentage rather than a vague label, is what makes an input clear.
A process is work that the program does with its data. Inputs arrive and outputs are produced, but in between the data has to be changed, checked or kept track of — that work is the process. Without processes, the data would just sit there.
One process often does two of these at once. Adding a new value to a running total is a calculation, and the answer has to be stored so the next value can be added to it. The running total grows as the algorithm runs, so it needs a variable to hold it; working it out once and throwing it away would lose everything added before.
A Year 10 tutor group runs a bake sale to raise money for an animal shelter, and every cake sells for £2. Each time a cake is sold, £2 is added to a running total — a calculation whose answer has to be stored in a variable. A second variable counts how many cakes have been sold, so its value goes up by one with every sale. After each sale the running total is compared with the group's £120 target, and the decision is whether to put out the “target reached” sign. At any moment, subtracting the running total from £120 shows how much money is still needed — a calculation that uses the stored total and the target.
An output is a result produced by a program or system — anything it sends out so that a person or another device can use it. Every output leaves the program in some form, which is why a program that shows nothing, saves nothing and controls nothing is of little use.
An output does not have to be seen. It can be shown on screen, printed on paper, saved to a file, played as sound, or sent as a signal to a device. The choice depends on who or what needs the result: a person reading it, a record kept for later, or a machine that must act.
| Type of output | Example |
|---|---|
| Shown on screen | The final score displayed at the end of a quiz round |
| Printed | A receipt printed for a repaired bicycle |
| Saved to a file | A club's attendance list stored so it can be opened again next week |
| Played as sound | A station announcement naming the next platform |
| Sent as a signal | A robot vacuum starting its cleaning cycle at a set time |
Some outputs carry no words or pictures at all. They are short signals sent to a device, and the device responds by turning itself on or off. This is useful because the program decides when the device should work, while the device supplies the physical effort. For example, a car park barrier stays down until a ticket has been paid; the program then sends a signal that raises the arm. If the signal is never sent, the arm stays where it is.
A file written quietly in the background and a signal sent to a device are just as much outputs as a score on screen, even though nobody watches them happen. Judge an output by whether it leaves the program and is used elsewhere, not by whether it appears on a display.
Every problem can be split into three roles: the inputs are the data the program is given or asks for, the processes are the actions it performs on that data, and the outputs are what it must produce.
Imagine a school reading challenge. A program asks for a pupil's name and the number of books they have read. If the number is 10 or more, it shows a Gold award message; otherwise it shows a Keep going message.
| Inputs | Processes | Outputs |
|---|---|---|
| the pupil's name the number of books the pupil has read |
compare the number of books with 10 decide which award message to use |
the award message (Gold award or Keep going) shown on screen |
To find each part, pick out every item of data the program is handed or asks for; those are inputs. Look for the work done on the data — a comparison, a total, a decision — and those are processes. Outputs are the things that must be displayed, printed, saved or used to control a device. Describe each one specifically: a label such as "the count" could mean anything, so write "the number of books read"; "the message" is vague, so name the award message.
If the challenge is changed so results are grouped by tutor group, the extra raw data you need is each pupil's tutor group. A group total is worked out from the book counts, so it is not an additional input. Only new data supplied from outside the program counts.
When a program asks you for data, two separate things happen: the program displays a message, and you type a reply. These two parts have different roles.
The message that asks you to enter data is called a prompt. A prompt is an output, because the program is showing it on screen. It is not the data itself — it is the instruction telling you what to type.
The data you type in response to the prompt is an input, because it enters the program from outside. For example, a bowling alley kiosk might display How many games will you play? — that message is the output. If you type 3, the value 3 is the input.
In Python, the input() function displays a prompt and returns a value. The text inside the brackets is shown on screen, so it is an output. The value the function returns is the text you typed, so it is an input. input() always returns text, so int() is used to convert it to a whole number before it is stored.
| Part | Example | Role |
|---|---|---|
| Message shown by the program | How many games will you play? | Output |
| Data typed by the user | 3 | Input |
# Bowling alley kiosk
games = int(input("How many games will you play? "))
print("Games booked: " + str(games))Output when the user types 3:
How many games will you play? 3
Games booked: 3The prompt is the output displayed by input(). The number you type is the input: int() turns the typed text into a whole number, which is stored in games.
Every Python program can be described by how it handles data. A call to input() takes data from the user, so it is an input. A call to print() displays data, so it is an output. The work that changes the input into the output is a process.
Processes include assignment with =, calculations such as adding or multiplying, and if statements that compare values and choose what happens next. Storing a value in a variable is work done by the program, so it is a process too. When an assignment line includes a call to input(), the call is the input and storing the typed value is also a process. The line as a whole is usually classed as an input, because it brings new data into the program.
battery = input("Battery %: ")
battery = int(battery)
after_charge = battery + 20
if after_charge >= 100:
print("Fully charged")
else:
print("Not full yet")Output when the user types 75:
Battery %: 75
Not full yet
Each line in the program has a role. Line 1 gets data from the user. Lines 2 to 4 convert, calculate and decide. The print() lines show a result.
| Line | Role | Reason |
|---|---|---|
| 1 | Input (with storage) | input() receives the battery percentage typed by the user; = stores it. |
| 2 | Process | int() changes the text to a whole number and = stores it. |
| 3 | Process | Adds 20 to the percentage and stores the result. |
| 4 and 6 | Process | The if compares values and chooses a branch; else is part of that decision. |
| 5 and 7 | Output | print() displays a message. |