Same idea, three forms
You can show a friend the way to the station in three ways. You can draw a map. You can say the steps. You can use a phone app.
An algorithm is the same. You can draw it as a flowchart. A flowchart is a picture made of shapes and arrows. You can write the steps in words. Or you can write code.
Key words
| Word | What it means | Example |
|---|---|---|
| Written description | An algorithm written as numbered steps in plain words. | 1 Ask for the age |
| Hierarchical marker | A number that shows the order and what belongs to what. | 1, 2, 2.1, 2.2, 3 |
| Code | Instructions in a programming language that a computer runs. | print("Hi") |
| Translate | Say the same algorithm in another way. | from a flowchart to code |
| Flowchart | A picture of an algorithm made of shapes and arrows. | start, check age, end |
| Selection | Choosing a path by asking a question. | if age is under 16 |
| Iteration | Repeating steps. | while n is more than 0 |
| Loop | Another name for iteration. | repeat while n is more than 0 |
The cells students fill in are left blank in this preview.
A bus fare in words
| Marker | Step |
|---|---|
| 1 | Ask for the age. |
| 2 | If the age is under 16: |
| 2.1 | The fare is 1. |
| 3 | Otherwise: |
| 3.1 | The fare is 2. |
| 4 | Show the fare. |
The cells students fill in are left blank in this preview.
Read the steps
Have a go
Read the steps
3 questionsWritten steps with markers
A written description uses numbers. The numbers show the order. A step with a number like 2.1 belongs to step 2.
This is a hierarchical marker. A whole number is a main step. A number with a dot is a smaller step inside it.
The same algorithm as a flowchart
The same algorithm as Python code
age = 12
if age < 16:
fare = 1
else:
fare = 2
print(fare)
# Change age to 30 and run it again.
Make a free account to save your work and track your progress.
More than two choices
Tip
Use elif for a second question. It means "else if". Python checks if, then each elif, then else. It runs only the first one that is true.
Translate step by step
| Flowchart | Written steps | Python |
|---|---|---|
| Input: age | 1 Ask for the age | age = int(input("Age? ")) |
| Decision: age under 16? | 2 If the age is under 16 | if age < 16: |
| Process: fare is 1 | 2.1 The fare is 1 | fare = 1 |
| Other arrow (no) | 3 Otherwise | else: |
| Output: fare | 4 Show the fare | print(fare) |
The cells students fill in are left blank in this preview.
Watch: one algorithm, three ways
Press Play on the short film below. It shows the bus fare as written steps, then as a flowchart and as Python. Each step has the same marker in every form. Then it follows a person aged 30 through the steps. Watch which step each line of Python is.
Film: one algorithm, three ways
One algorithm, three ways to write it
Every scene, as text
- A written description is an algorithm written as numbered steps in plain words. The bus fare: 1 Ask for the age. 2 If the age is under 16: 2.1 The fare is 1. 3 Otherwise: 3.1 The fare is 2. 4 Show the fare. A number with a dot is a smaller step inside: 2.1 belongs to step 2.
- The same algorithm as a flowchart, each step marked with its number from the written steps: 1 the input, age; 2 the decision, age under 16?; 2.1 on yes, fare is 1; 3 the no arrow, otherwise; 3.1 fare is 2; 4 the output, fare.
- The same algorithm as Python, each line marked with its step: 1 age = int(input("Age? ")); 2 if age < 16:; 2.1 fare = 1, indented inside the if; 3 else:; 3.1 fare = 2; 4 print(fare). As each step is read, a ring goes round it in every form.
- Follow a person aged 30 through every form. 1 The age is 30. 2 Is 30 under 16? No, so 2.1 is skipped. 3 Otherwise, 3.1 the fare is 2. 4 Show the fare: the program prints 2.
- Myth (false): a flowchart is the only way to show an algorithm. One algorithm can be a flowchart, numbered written steps or Python code. With a flowchart you can see the path quickly, anyone can read written steps, and a computer can run code. Choose the one that suits the reader.
Match the step to its Python line
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Good and bad points of each way
| Way | Good points | Bad points |
|---|---|---|
| Written steps | Quick to write. Anyone can read it. No tools needed. | Can be unclear. Long lists are hard to follow. It is easy to mix up loops and choices. |
| Code | A computer can run it. It is exact. You can test it. | You must know the language. A small spelling mistake stops it working. |
| Flowchart | You can see the path quickly. | Slow to draw. Big charts are hard to read. |
The cells students fill in are left blank in this preview.
Your turn: steps into Python
Each function has written steps in its comments. Turn the steps into Python.
- bus_fare uses selection: under 5 pays 0, under 16 pays 1, everyone else pays 2.
- countdown_text uses iteration: countdown_text(3) gives 3 2 1 Go!
- welcome uses sequence.
Press Run Tests to check.
def bus_fare(age):
# 1 If the age is under 5, the fare is 0.
# 2 Else if the age is under 16, the fare is 1.
# 3 Otherwise the fare is 2.
return None
def countdown_text(n):
# 1 Start with empty text.
# 2 While n is more than 0:
# 2.1 Add n and a space to the text.
# 2.2 Take 1 away from n.
# 3 Give back the text with Go! on the end.
return ""
def welcome(name):
# 1 Join "Hello, ", then the name, then "!" (an exclamation mark).
# 2 Add the words " Welcome."
# 3 Give back the text.
return ""
Make a free account to save your work, or press Start lab to work through the whole lesson.
Your turn: code into a flowchart
Here is a Python program. Turn it into a flowchart. Use the right shapes. Mark each arrow out of the decision. lives = 3 while lives > 0: print("Playing") lives = lives - 1 print("Game over")
flowchart TD
s([Start]) --> a[Set lives to 3]
a --> todo[Put the rest here]
Make a free account to save your work, or press Start lab to work through the whole lesson.
Five steps to design an algorithm
Tip
To design an algorithm: 1 Say what goes in and what comes out. 2 Write the steps in words. 3 Mark the choices and the repeats. 4 Draw or code it. 5 Test with an example.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
One algorithm can be a flowchart, numbered written steps or Python code. Each has good and bad points. Choose the one that suits the reader.