The bus reader makes a choice
You tap your card on the bus. The reader asks itself some questions. Is this a child? Is this a student? Is it after 9am?
Each answer changes the price. A program does the same. It follows steps in order. At some steps it makes a choice. In this lesson you learn both.
Key words
| Word | What it means | Example |
|---|---|---|
| Sequence | Steps that run one after another, from the top to the bottom. | print, then print |
| Selection | A choice. The program picks one path. | if age < 16 |
| Condition | A question with the answer True or False. | age < 16 |
| if | Runs its lines only when the condition is True. | |
| elif | Short for else if. It tests another condition when the ones above were False. | |
| else | Runs when no condition above was True. | |
| match case | Compares one value with a list of patterns and picks the first that fits. | |
| Indentation | The spaces at the start of a line. They show which lines belong inside the if. | |
| Flowchart | A picture of steps and decisions, drawn with symbols and arrows. |
The cells students fill in are left blank in this preview.
Sequence: one step after another
A sequence is a list of steps that run in order. Python starts at the top and goes down. It does not skip a line unless you ask it to.
Order matters. You cannot take your ticket before you tap your card.
Try it: steps in order
print("1. Tap your card")
print("2. The reader checks the fare")
print("3. Take your ticket")
# Swap line 1 and line 3, then run it again.
# The steps still run in order. But now the order is wrong.
Quick check: sequence
Have a go
Quick check: sequence
3 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
Selection: the program chooses
Selection means the program chooses a path. It looks at a condition. A condition is a question with the answer True or False.
If the answer is True, one path runs. If it is False, the program takes the other path.
Flowchart symbols
| Symbol | What it means |
|---|---|
| Rounded box | Start or end |
| Rectangle | A process: something to do |
| Rectangle with double sides | A sub-process: a part that has its own flowchart |
| Diamond | A decision: a question with two or more ways out |
| Parallelogram | Input or output |
| Small circle | A connector: joins to another part of the chart |
| Arrow | The flow: which step comes next |
The cells students fill in are left blank in this preview.
Try it: if and else
age = 14
if age < 16:
print("Child fare: 1.00")
else:
print("Adult fare: 2.50")
# Change age to 30 and run it again.