Python While Loops: Indefinite Iteration
This lesson introduces Year 10 students to indefinite iteration using Python 'while' loops. Students will learn the syntax, how to control loop execution using conditions, and how to avoid the common pitfall of infinite loops. The lesson covers both counter-controlled and sentinel-controlled loops.
Learning objectives
- Explain the purpose of a while loop as a form of indefinite iteration.
- Identify and fix common errors in while loops, such as infinite loops and off-by-one errors.
- Construct a Python program that uses a while loop to validate user input.
- Trace the value of variables through multiple iterations of a while loop.
Lesson plan (60 minutes)
Introduction · 5 min
Starter: Retrieval practice on 'If statements' and comparison operators (==, !=, <, >).
Teaching · 10 min
Teacher Explanation: Introduction to while loops, syntax, and the concept of 'indefinite iteration'.
Teaching · 10 min
Live Coding: Teacher demonstrates a basic counter loop and a user-input validation loop.
Independent Practice · 20 min
Differentiated Activity: Students work through Support, Core, or Stretch worksheets.
Plenary · 5 min
Debug Challenge: Students identify errors in 3 broken code snippets on the board.
Assessment · 10 min
Check for Understanding: 8-question multiple-choice quiz.
Key vocabulary
- Iteration
- Repeating a section of code multiple times.
- Indefinite Iteration
- A loop where the number of repeats is not known in advance.
- Infinite Loop
- A loop that never ends because the condition never becomes False.
- Sentinel Value
- A specific value used to signal the end of a loop (e.g., typing 'quit').
- Increment
- Increasing a variable by a set amount (usually 1).
Core worksheet (first questions)
- Explain the term 'indefinite iteration' in your own words.
- True or False: A 'while' loop will always run at least once. Explain your answer.
- Find the error: \nx = 10\nwhile x == 10:\n print("Hello")\n(The code prints Hello forever. Why?)
- Write a short program that asks a user for a password and keeps asking until they type "coding".
Quiz (sample)
1. When does a 'while' loop execute its block of code?
- A. Exactly ten times
- B. While the condition is True
- C. While the condition is False
- D. Until the user presses Enter
2. What is the technical term for a loop that repeats an unknown number of times?
- A. Definite iteration
- B. Indefinite iteration
- C. Selection
- D. Variable assignment
3. What happens if the condition in a while loop never becomes False?
- A. The program crashes immediately
- B. An infinite loop occurs
- C. The loop runs once and stops
- D. Python automatically fixes the error
The full pack is waiting
Support and Stretch worksheets, every answer and model answer, the teaching explainer and common misconceptions, editable and ready to print as PDF or Word.
Start your 14-day trialMore Computer Science lesson packs
- Year 8 Computer Science: Introduction to Python
- Year 8 Computer Science: Binary numbers
- Year 8 Computer Science: Algorithms and flowcharts
- Year 8 Computer Science: E-safety
- Year 10 Computer Science: Sorting algorithms
- Year 10 Computer Science: Networks and protocols
- Year 10 Computer Science: Boolean logic