Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

1.2 (2 of 3): One algorithm, three ways to write it

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
Written descriptionAn algorithm written as numbered steps in plain words.1 Ask for the age
Hierarchical markerA number that shows the order and what belongs to what.1, 2, 2.1, 2.2, 3
CodeInstructions in a programming language that a computer runs.print("Hi")
TranslateSay the same algorithm in another way.from a flowchart to code
FlowchartA picture of an algorithm made of shapes and arrows.start, check age, end
SelectionChoosing a path by asking a question.if age is under 16
IterationRepeating steps.while n is more than 0
LoopAnother 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

MarkerStep
1Ask for the age.
2If the age is under 16:
2.1The fare is 1.
3Otherwise:
3.1The fare is 2.
4Show the fare.

The cells students fill in are left blank in this preview.

Read the steps

Have a go

Read the steps

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Written 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

A flowchart: Start, then Input: age, then the question Age under 16?, where yes goes to Fare is 1 and no goes to Fare is 2; both go to Output: fare, then End.A flowchart: Start, then Input: age, then the question Age under 16?, where yes goes to Fare is 1 and no goes to Fare is 2; both go to Output: fare, then End.

The same algorithm as Python code

Code: python
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

FlowchartWritten stepsPython
Input: age1 Ask for the ageage = int(input("Age? "))
Decision: age under 16?2 If the age is under 16if age < 16:
Process: fare is 12.1 The fare is 1 fare = 1
Other arrow (no)3 Otherwiseelse:
Output: fare4 Show the fareprint(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

  1. 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.
  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.
  3. 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.
  4. 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.
  5. 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

WayGood pointsBad points
Written stepsQuick 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.
CodeA computer can run it. It is exact. You can test it.You must know the language. A small spelling mistake stops it working.
FlowchartYou 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.

  1. bus_fare uses selection: under 5 pays 0, under 16 pays 1, everyone else pays 2.
  2. countdown_text uses iteration: countdown_text(3) gives 3 2 1 Go!
  3. welcome uses sequence.

Press Run Tests to check.

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.