← Computing
ComputingCoding: from blocks to Python20 min★★ difficulty

Python decisions and loops

Now we give Python choices with if, and make it repeat with for and while, just like the Repeat block.

Part of your national curriculum
  • Algorithms and programming: Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems
  • Algorithms and programming: Understand simple algorithms, expressed using sequence, selection and iteration, and correct errors in algorithms

Lesson overview

What you'll learn in this lesson

Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems

Key learning points

  • • Making a decision
  • • = and ==
  • • for loops
  • • while loops

This lesson at a glance

  • 20 minutes
  • 18 parts to scroll through
  • 4 quick checks
  • Marked quiz at the end
  • Gentle pace: short sittings with pauses

Words to know

programminglanguagestextualvarietycomputational

Scroll down — the lesson carries on below

Part 1 of 18 · Intro6%
1

Introduction

Part 1 of 18

Python decisions and loops illustrationVisual introduction

Picture this

Python decisions and loops

Now we give Python choices with if, and make it repeat with for and while, just like the Repeat block.

In a nutshell

Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems

2

What you already know

Part 2 of 18

Before we start

What you already know

You can use print() and variables.

3

Key words

Part 3 of 18

Key words

Words you'll need today

if / else
Runs code only when a condition is true, otherwise runs the else part.
==
Checks whether two things are equal (two equals signs).
for loop
Repeats code a set number of times, e.g. for i in range(5).
indentation
The spaces at the start of a line that show which code is inside an if or loop.
4

Learning cycle

Part 4 of 18

Learning cycle 1 of 2

Part 1: If and else

A short piece of teaching, then a check to make sure it has landed.

5

Explore the idea

Part 5 of 18

Learn

Making a decision

if score >= 50: print("Pass") else: print("Try again") The colon and the 4-space indent tell Python which lines belong to the if.

6

Explore the idea

Part 6 of 18

Learn

= and ==

One = stores a value. Two == compares. if animal == "snow leopard": checks whether they match.

7

Quick check

Part 7 of 18

Quick check

Which checks if two values are equal?

8

Reset break

Part 8 of 18

Pause

That's sitting 1 of 2 done

Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.

Stop here for now
9

Quick check

Part 9 of 18

Quick check

What shows which lines are inside an if?

10

Learning cycle

Part 10 of 18

Learning cycle 2 of 2

Part 2: Loops

A short piece of teaching, then a check to make sure it has landed.

11

Explore the idea

Part 11 of 18

Learn

for loops

for i in range(5): print(i) shows 0, 1, 2, 3, 4. range(5) counts five numbers starting at 0. This is the Repeat block in Python.

12

Explore the idea

Part 12 of 18

Learn

while loops

while lives > 0: keeps going until the condition becomes false. Make sure something inside changes, or the loop never ends.

13

Quick check

Part 13 of 18

Quick check

How many times does for i in range(3): run?

14

Quick check

Part 14 of 18

Quick check

A while loop with nothing changing inside will:

15

Common mix-ups

Part 15 of 18

Common mix-ups

Things people often get wrong

  • range(5) gives 0 to 4, not 1 to 5.
  • Missing indentation is the most common Python error. Lines inside an if or loop must be indented.
16

Challenge round

Part 16 of 18

Game · Sort it

Which of these are true?

Drag each card into the right column. Tap a card first if dragging is fiddly.

True

Not true

17

Mastery quiz

Part 17 of 18

Marked quiz

End of lesson quiz: Python decisions and loops

4 questions, marked with the reasoning shown. No timer.

  1. 1. The Python version of a Repeat block is a:

  2. 2. What's the first number range(4) gives?

  3. 3. if age >= 13: runs when age is:

  4. 4. An algorithm repeats 'move forward' until a wall is reached, then turns right. Which programming ideas does it use?

18

Lesson round-up

Part 18 of 18

Lesson round-up

Ready when you are

Quiz score

Not sat

Games

Not played

Points this lesson

0

Best run

0 in a row

Luna: 0 out of 4 on the practice checks. Only if you feel up to it — one more?

Ask Luna

Do the work

Your written work for this lesson

This is the part you'd do in your book at school. Pick the pieces below, write here or on paper, and photograph anything you draw or work out.

Article

How a supermarket checkout knows the price

When a barcode is scanned, the checkout does not store the price itself. It sends the barcode number to a database and reads back a record: product name, price, whether it needs an age check. The record is the single place the price lives, so changing one row changes every till in the shop at once.

Problems come from bad data, not bad scanners. If two products share a barcode, or a price update reaches the shop overnight but the till holds an old copy in its cache, customers are charged the wrong amount. Shops check this with test transactions: scan a known item, expect a known price, record what actually happened.

Good systems also log every scan. If a customer complains, the log shows exactly which record was used and when.

Written for Good Learning Co.

Reading questions

  1. Write in your own words what the checkout sends and what it receives back.
  2. Explain one way bad data could cause a wrong price, and how logging helps find it.
  3. Design a test table with four tests for a new checkout, with expected and actual results.
  • Reading and notes

    Reading

    Read the lesson text again and note the key terms and how each one is used, in your own words — as you would for revision.

    Half a page of notes

  • Extended writing

    Writing

    Explain a real system that uses this idea — how it works, where it can go wrong, and how you would make it safer or better.

    250–350 words

  • Algorithm on paper

    Diagram

    Write your steps out as pseudocode or a flowchart before you type anything, then photograph it.

    1 flowchart or 10 lines

  • Code write-up

    Writing

    Copy your code in, then explain underneath what each part does and what you had to fix.

    Code + 5 sentences

  • Test table

    Working out

    Make a test table: what you tried, what you expected, what actually happened.

    4–6 tests

Saved as you type.

Part 1 of 18 · Introduction

Playground

Python Lab

Write and run real Python

Pick a challenge, read it, predict what it will print, then press Run. Change something and run again.

How this is going

  • Not looked at yet

    Use two or more programming languages, at least one of which is textual, to solve a variety of computational problems

  • Not looked at yet

    Understand simple algorithms, expressed using sequence, selection and iteration, and correct errors in algorithms

Good Learning Co. — personalised UK curriculum learning, built around what each learner loves.