Year 8 · Spring 2
Python I · Lesson 4 of 5
while loops
PracticalTopic 13 Programming basics
Year 8 · Python I · Lesson 4 of 5
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.
Year 8 · Python I · Lesson 4 of 5
Success criteria
You have got it when you can say:
- 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.
Year 8 · Python I · Lesson 4 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
score = 45. Code: if score >= 25: print("Bronze") elif score >= 40: print("Gold"). What prints?
A GoldB BothC Bronze
C. Bronze
- Last unit
Shifting a binary number one place right…
A Adds 1B Halves itC Doubles it
B. Halves it
- Earlier this year
RAM is volatile. That means…
A It loses everything when power is offB It can catch fireC It is very fast
A. It loses everything when power is off
Year 8 · Python I · Lesson 4 of 5
Key words (1 of 2)
- 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").
Year 8 · Python I · Lesson 4 of 5
Key words (2 of 2)
- 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 · step 1 of 2
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.
Scratch: 'repeat 5'.
The explanation · step 2 of 2
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.
If the condition can never become false, it loops for ever: an infinite loop.
Year 8 · Python I · Lesson 4 of 5
Quick quiz · 1 of 3
count = 5, then while count > 0: print(count). What happens?
- A Prints nothing
- B Prints 5 4 3 2 1
- C Prints 5 for ever
C. Prints 5 for ever
Nothing changes count, so count > 0 stays True: an infinite loop.
Year 8 · Python I · Lesson 4 of 5
Quick quiz · 2 of 3
What does print("3" + "4") show?
- A An error
- B 34
- C 7
B. 34
They're strings, so + joins them: 34.
Year 8 · Python I · Lesson 4 of 5
Quick quiz · 3 of 3
What does 17 % 5 give?
- A 2
- B 3
- C 3.4
A. 2
17 ÷ 5 = 3 remainder 2. % gives the remainder.
Year 8 · Python I · Lesson 4 of 5
Exit ticket
Answer on paper before you go. Then check.
range(3) gives…
A 0, 1, 2B 0, 1, 2, 3C 1, 2, 3
A. 0, 1, 2 Starts at 0, stops before 3.
x = input("Number: ") and the user types 5. What type is x?
A intB strC float
B. str input() always gives a string: "5". Cast it with int() to do maths.