Skip to the page
Year 8 · Spring 2 · Python I

while loops

PracticalPresent ▶

What this lesson covers

Topic 13 Programming basics

KS3 Computing · A program stores information in variables, works on it with operators, and controls the order with sequence, selection and iteration. Examples are in Python, with Scratch alongside for Year 7.

ProgrammingCambridge Lower Secondary Computing 0860

You will learn to

  • Explain that a while loop repeats as long as its condition is true.
  • Use a while loop to keep asking for a password until it is right.
  • Create a countdown from 10 to 1 with a while loop.
  • Identify why a loop never ends, and fix it.

Success criteria

  • I can write a password checker that only stops when the password is right.
  • I can make a countdown that stops at the right number.
  • I can explain what an infinite loop is, and stop one with the Stop button.

Key words

Say each word, then read what it means. Every one is in the glossary too.

infinite loop
A loop whose condition never becomes false.
Variable
A named place in memory that holds a value.
Assignment
Storing a value in a variable. = in Python, ← in pseudocode.
Casting
Changing a value from one data type to another, like int("13").
Concatenation
Joining strings together: "7" + "7" = "77".
Selection
Choosing a path with if / else.
Iteration
Repeating steps: a loop.
Boolean
A value that is only True or False.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Loops: for

for i in range(5):
    print("Lap", i)

Prints Lap 0 to Lap 4. range(5) gives 0, 1, 2, 3, 4: it starts at 0 and stops before 5.

e.g.Example

Scratch: 'repeat 5'.

Loops: while

password = ""
while password != "dragon":
    password = input("Password: ")
print("Access granted")

Repeats while the condition is true. As soon as the user types dragon, the loop ends.

!Watch out

If the condition can never become false, it loops for ever: an infinite loop.

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.