← ACARA Digital Technologies Years 7–8

Quiz program using a function, a loop and a score

Calls a function that returns True for a right answer, then adds 1 to score when it does. Put this in a loop to ask every question and total the quiz score.

Understanding Quiz program using a function, a loop and a score in ACARA Digital Technologies Years 7–8

What it does: The function returns True for a right answer, so the if runs score += 1 only when the answer was correct. += adds to the variable. == only compares values, so score never changes. If score = 0 is inside the loop, the score resets to 0 on every question. == compares two values and gives True or False. A single = is assignment and causes a SyntaxError. Parameters let you pass in a different question and answer each time you call the function. Without the f before the quote, Python prints the text {score} instead of the number. The == comparison makes a True or False value. return sends that value back so an if statement can use it.

Common mistakes: The input() call is missing its closing bracket. It should be input(q + ' ') so the question is shown and the student's answer is stored in guess. A function definition must end with a colon. The first line should be def ask(q, a): so Python knows the indented lines are the function body.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 522–560) and word search (Modules 522–580).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line adds 1 to the score when the answer is right?
if ask(q, a): score == 1if ask(q, a): score += 1
Matches
if ask(q, a): score += 1
↓
score += 1 is not indented, so it is not inside the if block and Python raises an IndentationError.
Applying
Meanings
Term
if ask(q, a): score += 1
Meaning
Calls a function that returns True for a right answer, then adds 1 to score when it does. Put this in a loop to ask every question and total the quiz score.
Practice
def ask(question, answer, reply):
Testing
Gaps
ask(question, answer):
Check Answer
Spots
1def ask(q, a):
2 guess = input(q + ' '

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Sea Dragon' badge — OOP Pioneer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line adds 1 to the score when the answer is right?
A. if ask(q, a): score += 1 ✓
B. if ask(q, a): score == 1
+= adds to the variable. == only compares values, so score never changes.
2. What should a quiz function like ask(q, a) return?
A. return True or False ✓
B. return the whole quiz score
The function checks one question and reports right (True) or wrong (False). The score lives outside the function.
3. Where should score = 0 go in a quiz program?
A. Before the loop starts ✓
B. Inside the loop
If score = 0 is inside the loop, the score resets to 0 on every question.
4. Which function checks an answer correctly?
A. def ask(q, a): return input(q) = a
B. def ask(q, a): return input(q) == a ✓
== compares two values and gives True or False. A single = is assignment and causes a SyntaxError.
5. Why does the quiz function use parameters q and a?
A. So the function can only ask one question
B. So the same function can ask any question ✓
Parameters let you pass in a different question and answer each time you call the function.

Match the Pairs

def ask(question, answer): → Defines a function that takes a question and its correct answer as parameters
return reply == answer → Gives back True if the player's reply matches the answer, otherwise False
Add 1 to the score each time the player gets a question right → if ask(q, a): score += 1
Show the final score after the loop ends using an f-string → print(f"You scored {score}")
if ask(q, a): score += 1 → score += 1 is not indented, so it is not inside the if block and Python raises an IndentationError.
for q, a in questions: score = 0 if ask(q, a): score += 1 → score = 0 is inside the loop, so the score resets to 0 on every question and the total is lost.

Key Term

if ask(q, a): score += 1
Calls a function that returns True for a right answer, then adds 1 to score when it does. Put this in a loop to ask every question and total the quiz score.

Fill in the Blanks

1. def ask(question, answer): return input(question) == answer
Hint: Type the keyword that starts a function definition.
2. def ask(q, a): return input(q) == a
Hint: Type the comparison operator that checks if the typed reply equals the answer. It gives True or False.
3. for q, a in quiz: if ask(q, a): score += 1
Hint: Type the operator that adds to score and stores the new value.
4. score = 0 for q, a in quiz: if ask(q, a): score += 1
Hint: Type the keyword that starts a loop so every question gets asked.
5. print(f"Score: {score}/{len(quiz)}")
Hint: Type the variable name that counts the correct answers, placed inside the curly braces.

Spot the Error

Question 1
A def ask(q, a):
B guess = input(q + ' '
C return guess == a
D score = 0
Line B — The input() call is missing its closing bracket. It should be input(q + ' ') so the question is shown and the student's answer is stored in guess.
Question 2
A def ask(q, a)
B guess = input(q)
C return guess == a
D score = 0
Line A — A function definition must end with a colon. The first line should be def ask(q, a): so Python knows the indented lines are the function body.
Question 3
A quiz = [('2+2', '4')]
B score == 0
C for q, a in quiz:
D if ask(q, a):
E score += 1
Line B — A double equals sign compares values, it does not store one. The score must start with the assignment score = 0 so that score += 1 can work later.
Question 4
A score = 0
B for q, a in quiz:
C if ask[q, a]:
D score += 1
Line C — A function is called with round brackets, not square brackets. It should be if ask(q, a): so the function runs and returns True or False.
Question 5
A def ask(q, a):
B guess = input(q + ' ')
C return guess = a
D print(ask('2+2', '4'))
Line C — A single = is assignment and cannot be used in a return. To compare the guess with the answer and return True or False, use ==: return guess == a.

Student with a class code? Join your class →