Year 10Technology 60 min
We are learning to
- We are learning to use lists and a loop so a program can repeat the same steps for different data.
- We are learning to make a program forgiving of the way people type, and to test it with inputs designed to break it.
Success criteria: I can…
- I can store questions and answers in two lists and use an index to match them up.
- I can loop through the lists with for and range(len(...)).
- I can clean user input with strip() and lower() before comparing it.
- I can keep a score and give feedback using if, elif and else.
- I can write test cases with expected outputs, including inputs that might break my quiz.
Key competenciesThinkingManaging self
Lesson sequence
60 min total- 1
One question, one bug
Starter 0–8 minTeacher- Type and run
- reply = input("What is the capital city of New Zealand? ")
- if reply == "Wellington":
- print("Correct!")
- else:
- print("Not quite. The answer was Wellington.")
- Run it three times, typing Wellington, then wellington, then Wellington with a space after it.
- Ask: Which ones were marked wrong? (the second and third)
- Ask: Why? (== compares text exactly, so a small w or an extra space makes it different)
- Say: “"We'll fix that later. First: what would we need to do to add four more questions this way?" (copy and change the block four more times)”
- Ask: What's the problem with that? (long, repetitive code; a change to how marking works has to be made five times)
Students- Predict each result before it runs.
- Explain why two correct answers were marked wrong.
Teacher's editor on the screen - 2
Lists and indexes
Main 8–18 minTeacher- Show
- questions = ["What is the capital city of New Zealand?",
- "How many sides does a hexagon have?",
- "What gas do plants take in for photosynthesis?",
- "What is 7 x 8?",
- "Which planet is closest to the Sun?"]
- answers = ["Wellington", "6", "carbon dioxide", "56", "Mercury"]
- Say: “The two lists line up: the answer at each position belongs to the question at the same position.”
- Questions
- 1.What is questions[0]? ("What is the capital city of New Zealand?")
- 2.What is answers[3]? ("56")
- 3.What is len(answers)? (5)
- 4.What does answers[5] give? (an IndexError, because the last index is 4)
- Say: “"Why is 6 in quote marks?" (input() always gives text, so we compare text with text)”
Students- Type the two lists into a new file called quiz.py.
- Answer the four index questions, then check them using print().
Lists on the screen - 3
Loop, clean and score
Main 18–38 minTeacher- Add under the lists, explaining each part
- score = 0
- print("Welcome to the quiz! Answer 5 questions.")
- for i in range(len(questions)):
- reply = input(questions[i] + " ")
- if reply.strip().lower() == answers[i].lower():
- print("Correct!")
- score = score + 1
- else:
- print("Not quite. The answer was " + answers[i] + ".")
- print("You scored " + str(score) + " out of " + str(len(questions)) + ".")
- Ask: What values does i take? (0, 1, 2, 3, 4)
- Ask: Why lower() on both sides? (so neither the user's capitals nor ours matter; we can keep 'Wellington' capitalised for the feedback message)
- Ask: Why is the score line not indented? (it prints once, after all five questions)
- Students type and run it.
- Test case 1: type " WELLINGTON ", 6, Carbon Dioxide, 54, mercury. Expected: four Correct! and one "Not quite. The answer was 56.", then You scored 4 out of 5.
- Tip: If the score is always 0 or 1, check that score = 0 is above the loop and score = score + 1 is indented inside the if.
Students- Add the loop and scoring code and run test case 1.
- Write down the actual output and compare it with the expected output.
Loop code on the screen - 4
Feedback and break-it testing
Main 38–50 minTeacher- Add at the end (not indented)
- if score == len(questions):
- print("Perfect score!")
- elif score >= 3:
- print("Well done!")
- else:
- print("Keep practising!")
- Ask: Why score == len(questions) rather than score == 5? (it still works if we add or remove questions)
- Test case 2: Auckland, six, oxygen, 56, Venus. Expected: only one Correct! (for 56), You scored 1 out of 5. then Keep practising!
- Say: “Your job now: write two more test cases that try to break the quiz, and record what happens.”
- Ask: Our quiz marked 'six' wrong. Is that fair? How could we fix it? (accept either answer, for example by storing a list of allowed answers for that question and checking with in)
- Students then change all five questions to a topic of their choice and retest.
Students- Add the feedback block and run test case 2.
- Write and run two break-it tests.
- Rewrite the questions for their own topic and test again.
Test table: input, expected output, actual output, pass or fail - 5
Exit check
Plenary 50–60 minTeacher- Students answer on an exit slip without running code:
- Questions
- 1.A user types " mercury" for the last question. Is it marked correct? Why? (yes: strip() removes the space and lower() makes both sides "mercury")
- 2.What score and feedback does a user get with 3 correct answers? (You scored 3 out of 5. then Well done!)
- 3.A student adds a sixth question but forgets to add its answer. What happens? (on the sixth time round the loop, answers[5] doesn't exist, so the program crashes with an IndexError)
- 4.Give one reason using lists and a loop is better than writing five separate if blocks. (the marking code is written once, so changes are made in one place and adding questions only means adding to the lists)
- Students save quiz.py.
Students- Answer the four exit questions alone.
- Save their quiz.
Exit slips
Key vocabulary
list
A collection of values stored in order under one name, written in square brackets: answers = ["Wellington", "6"].
index
The position of an item in a list. Python counts from 0, so answers[0] is the first item.
len()
Gives the number of items in a list, so len(questions) is 5 for five questions.
strip()
Removes spaces from the start and end of text, so " Mercury " becomes "Mercury".
lower()
Changes text to lower case, so "WELLINGTON" becomes "wellington", which makes comparisons ignore capital letters.
test case
A planned input with the output you expect, used to check that a program works.
Differentiation
Support
- Give a printed copy of the complete program with each part labelled (lists, loop, cleaning, scoring, feedback) and indentation shown as dots.
- Let students keep the five given questions and focus on getting both test cases to pass before changing topics.
- Give a trace table for the first two times round the loop (i, questions[i], reply, cleaned reply, score) to fill in with test case 1.
Extension
- Accept more than one answer per question: make answers a list of lists (for example ["6", "six"]) and check with if reply.strip().lower() in [a.lower() for a in answers[i]].
- Replace the two lists with a list of pairs, or a dictionary, and explain which version is easier to keep correct when adding questions.
- Shuffle the question order each time the quiz runs using the random module, keeping each answer matched to its question, and explain how you kept them matched.
Accessibility
- Increase the editor font size and keep the full program on the screen so students can compare line by line.
- Provide an error guide for today's likely errors: IndexError (list too short), IndentationError and NameError from a misspelt variable.
- Pre-teach list, index, loop and test case with a short real-life example of each for English language learners (a numbered shopping list works well for index).
Assessment
- Starter: students explain why == rejects "wellington".
- Index questions in step 2 show whether students understand zero-based indexes.
- Test cases 1 and 2 give exactly the expected outputs.
- Break-it tests: a secure student chooses inputs that test a real weakness (extra spaces, a word instead of a number, an empty answer) and records the result.
- Exit question 3 shows whether students understand that the lists must stay the same length.
Resources
- Devices with Python IDLE or a free online editor (Programiz or Trinket)
- Code for each step on the screen
- Test table, one per student
- Exit slips