




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Don't start with syntax. Start with the problem. What if a task needs to happen 10 times? What if it needs to happen 10,000 times? How can we avoid repeating the same code? How do we decide when repetition should stop? Key idea: A loop allows a program to repeat instructions without writing the same instructions again.
Before writing code, ask: “What part of this problem is repeating?” Example 1: Display the numbers from 1 to 10. Think:
Cover/ Example 2: for number in range( 1 , 11 ): print(number) Then explain how to think about it, rather than just what each word means. What is changing? Where does it start? Where does it stop?
How many times does the body execute?
Break this into/ Example 3: range(stop) range(start, stop) range(start, stop, step) Include off-by-one errors, because these are important programming-thinking mistakes. Example 4: range( 1 , 10 ) does not include 10. Ask: “If I need 1–10, what should my stopping value be?”
Example 5: number = 1 while number <= 10 : print(number) number += 1 Focus on the programmer's reasoning: Initial state → condition → action → state change → condition again This is a much better way to understand while loops than memorising syntax.
Give yourself decision questions:
for number in range( 1 , 21 ): if number % 2 == 0 : print(number) Then ask: What is being repeated? What is being tested? What happens when the condition is true? What happens when it is false?
These are important programming patterns. Counter: count = 0 for number in numbers: if number > 10 : count += 1 Accumulator: total = 0 for number in numbers: total += number Then explain the underlying pattern: Initialise → repeat → update → use result
This is a very useful “thinking like a programmer” section. Given/ Example 7: total = 0
for number in range( 1 , 4 ): total += number Trace it manually: Step number total Start — 0 1 1 1 2 2 3 3 3 6 Then ask: What will the program output? This helps you understand code rather than simply memorising it.
Show the mistake/Example 8: number = 1 while number <= 10 : print(number) Then ask: What changes between iterations? Nothing. Therefore, the condition remains true. Fix/Example 9: number = 1 while number <= 10 : print(number) number += 1
Introduce the idea:
Problem Typical cause How to think about it Infinite loop Condition never becomes false What changes each iteration? Wrong number of repetitions Incorrect range() What values are produced? Off-by-one Incorrect start/stop Is the stop value included? Unexpected output Incorrect indentation Which statements belong inside the loop? Loop stops too early Incorrect break Under what condition should it stop?
Instead of just giving examples, finish with problems where you must design the solution. Level 1 Print numbers 1–20. Print only even numbers. Calculate the sum from 1–100. Level 2 Count how many numbers in a list are greater than 50. Find the largest number without using max(). Keep asking for a number until the user enters a positive number. Level 3 Create a number guessing game. Create a simple menu that keeps running until the user chooses "Exit". Process a list of marks and calculate how many students passed. For each challenge, use: Problem → Plan → Pseudocode → Python → Test → Debug
That is much closer to “thinking like a programmer” than simply listing Python commands.