Year 8Technologies 60 minAC9TDI8P09: implement, modify and debug programs involving control structures and functions in a general-purpose programming language
We are learning to
- We are learning to find and fix syntax, runtime and logic errors in a Python program.
- We are learning to modify a program by changing a function and writing a new function that returns a value.
Success criteria: I can…
- I can read a Python error message and find the line and the type of error.
- I can explain the difference between a syntax error, a runtime error and a logic error.
- I can work out the expected output by hand and use it to spot a logic error.
- I can add a new branch to an if-elif-else inside a function.
- I can write a function with a parameter, a loop and a return value, and call it twice.
Key competenciesDigital LiteracyCritical and Creative ThinkingNumeracy
Lesson sequence
60 min total- 1
What should it print?
Starter 0–8 minTeacher- Display the points rules: 1st place 5 points, 2nd 3, 3rd 2, any other place 1 point for competing.
- Say: “Red House's places today were 1, 3, 2, 5, 1, 4. Work out their total by hand.”
- Questions
- 1.How many points for the place 5? (1, it is any other place)
- 2.What is Red's total? (5 + 2 + 3 + 1 + 5 + 1 = 17)
- Say: “Write 17 down. That is our expected output. A program is only correct if its output matches what we expect.”
Students- Calculate the expected total by hand.
- Record the expected output: Total points: 17.
Points rules on the board - 2
Fix the bugs one at a time
Main 8–28 minTeacher- Students open the buggy program (Resources) and run it.
- Say: “Python reads the whole file before running it, so syntax errors show first. Fix one error, run again, repeat.”
- Model reading the first message together: SyntaxError: expected ':' on line 2. Ask: "What is missing?" (the colon after def points_for_place(place))
- Students work in pairs to fix the rest and record each bug in the bug log (Resources).
- Expected sequence
- 1.Line 2, SyntaxError: expected ':'. Fix: add a colon to the def line.
- 2.Line 7, SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? Fix: elif place == 3:
- 3.Line 16, NameError: name 'totl' is not defined. Did you mean: 'total'? Fix: print("Total points:", total)
- 4.No error message, but the output is Total points: 1, not 17. Ask: "Why 1?" (each time round the loop, total is replaced, so it ends with the points for the last place, 4, which is 1) Fix: total = total + points_for_place(p)
- Ask: Which bug was hardest to find, and why? (the logic error, because Python gave no message; we only found it by comparing with 17)
- Tip: ask pairs to add print(p, total) inside the loop to watch the total change; remove it once the bug is fixed.
Students- Run, read each error, fix it and run again.
- Record each bug's line, type and fix in the bug log.
- Confirm the output is Total points: 17.
Buggy program file carnival_points.py (code in Resources) Bug log (template in Resources) Computers with a Python 3 editor - 3
Modify: a new rule and a new function
Main 28–50 minTeacher- Set Modification 1: a place of 0 means the student did not compete, so it scores 0 points.
- Ask: Where must the new branch go? (before the else, so 0 does not fall through to return 1)
- Students add: if place == 0: return 0, and change the old if place == 1 to elif. Test: print(points_for_place(0)) should print 0.
- Set Modification 2: write a function house_total(places) that loops through a list of places and returns the total. Model the first line only: def house_total(places):
- Say: “Your loop from before goes inside the function, and the function must return total at the end, not print it.”
- Students then call it for two houses and announce the winner with if-elif-else:
- red = house_total([1, 3, 2, 5, 1, 4])
- blue = house_total([2, 2, 0, 1, 3])
- Expected output: Red: 17, Blue: 13, Red wins.
- Questions
- 1.Work out Blue by hand. (3 + 3 + 0 + 5 + 2 = 13)
- 2.Why is a function better than copying the loop twice? (write it once, fix it once, reuse it for every house)
- Tip: if a pair gets 5 for Red, their return total is indented inside the loop, so the function returns after the first place.
Students- Add the place 0 rule and test it.
- Write house_total, call it for Red and Blue, and print the winner.
- Check the output against the hand-calculated totals.
Modification card (text in Resources) Model solution for the teacher (Resources) - 4
Name that error
Plenary 50–60 minTeacher- Questions
- 1.The program will not start and Python says expected ':'. What type of error is it? (syntax error)
- 2.The program crashes halfway with NameError. What type? (runtime error)
- 3.The program runs but Blue's total is 15 instead of 13. What type, and how did you find it? (logic error, by comparing with the hand-calculated expected output)
- 4.What does return do in house_total? (sends the total back to the line that called it, so it can be stored in red or blue)
- 5.Gold House placed 1, 1, 4, 0, 6. What will house_total return? (5 + 5 + 1 + 0 + 1 = 12)
- Students run question 5 in their program to check.
Students- Answer the questions.
- Test Gold House in their program and confirm 12.
Key vocabulary
function
a named block of code that does one job; it can take inputs (parameters) and give back a result with return
parameter
a variable in a function's definition that receives the value passed in, such as place
return
sends a value back from a function to the code that called it
syntax error
code that breaks Python's grammar rules, so the program cannot start
runtime error
an error that stops the program while it is running, such as using a variable that does not exist
logic error
the program runs but gives the wrong result
accumulator
a variable that builds up a running total inside a loop
Differentiation
Support
- Give students the bug log with the line numbers already filled in, so they focus on reading the message and choosing the fix.
- For Modification 2, provide the house_total function with blanks: def house_total(places): total = ____; for p in places: total = ____; return ____.
Extension
- Add a third house, Gold, with places [1, 1, 4, 0, 6], and change the winner code so it works for three houses (Red 17, Blue 13, Gold 12, so Red wins).
- Make points_for_place return 0 and print Invalid place for any negative place, and write three test calls with their expected outputs.
- Rewrite points_for_place using a dictionary {0: 0, 1: 5, 2: 3, 3: 2} and the get method with a default of 1, as in points.get(place, 1),, and explain which version is easier to change.
Accessibility
- Increase the editor font size and turn on line numbers so students can match error messages to lines.
- Print the buggy program with line numbers for students who prefer to annotate on paper before typing.
- For EAL/D learners, pre-teach syntax, runtime and logic with one short example of each on the board.
Assessment
- Bug log: each pair records all four bugs with the correct line, type (two syntax, one runtime, one logic) and fix.
- Program check: points_for_place(0) returns 0; house_total returns 17 for Red and 13 for Blue; the program prints Red wins.
- Plenary: students who classify all three error types and correctly predict Gold's total of 12 meet the success criteria.
Resources
- Buggy program carnival_points.py (type or paste exactly, keeping the 4-space indents; line numbers are for reference): 1 # Sports carnival points calculator (contains 4 bugs) / 2 def points_for_place(place) / 3 if place == 1: / 4 return 5 / 5 elif place == 2: / 6 return 3 / 7 elif place = 3: / 8 return 2 / 9 else: / 10 return 1 / 11 (blank line) / 12 places = [1, 3, 2, 5, 1, 4] / 13 total = 0 / 14 for p in places: / 15 total = points_for_place(p) / 16 print("Total points:", totl)
- Bug log template: columns Line | Error message (or wrong output) | Type: syntax, runtime or logic | Fix. Answers: line 2 | SyntaxError: expected ':' | syntax | add the colon. Line 7 | SyntaxError: invalid syntax, maybe you meant '==' | syntax | use ==. Line 16 | NameError: name 'totl' is not defined | runtime | change totl to total. Line 15 | output Total points: 1, expected 17 | logic | total = total + points_for_place(p).
- Modification card: 1. A place of 0 means did not compete and scores 0 points. Add this rule to points_for_place and test it with print(points_for_place(0)). 2. Write a function house_total(places) that returns the total points for a list of places. 3. Use it to work out red = house_total([1, 3, 2, 5, 1, 4]) and blue = house_total([2, 2, 0, 1, 3]), print both totals, and use if-elif-else to print Red wins, Blue wins or It's a tie.
- Model solution (teacher): def points_for_place(place): / if place == 0: return 0 / elif place == 1: return 5 / elif place == 2: return 3 / elif place == 3: return 2 / else: return 1. def house_total(places): / total = 0 / for p in places: total = total + points_for_place(p) / return total (indented level with for, not inside it). red = house_total([1, 3, 2, 5, 1, 4]); blue = house_total([2, 2, 0, 1, 3]); print("Red:", red); print("Blue:", blue); if red > blue: print("Red wins") elif blue > red: print("Blue wins") else: print("It's a tie"). Output: Red: 17, Blue: 13, Red wins.
- Computers with a Python 3 editor (IDLE, Thonny or a school-approved online editor)