Quiz program using a function, a loop and a score

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Quiz program using a function, a loop and a score

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. What should a quiz function like ask(q, a) return?
A return True or False
B return the whole quiz score
3. Where should score = 0 go in a quiz program?
A Before the loop starts
B Inside the loop
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

if ask(q, a): score += 1

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ ask(question, answer): return input(question) == answer
2.
def ask(q, a): return input(q) __ a
3.
for q, a in quiz: if ask(q, a): score __ 1
4.
score = 0 ___ q, a in quiz: if ask(q, a): score += 1
5.
print(f"Score: {_____}/{len(quiz)}")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def ask(q, a):
B guess = input(q + ' '
C return guess == a
D score = 0
Question 2
A def ask(q, a)
B guess = input(q)
C return guess == a
D score = 0
Question 3
A quiz = [('2+2', '4')]
B score == 0
C for q, a in quiz:
D if ask(q, a):
E score += 1
Question 4
A score = 0
B for q, a in quiz:
C if ask[q, a]:
D score += 1
Question 5
A def ask(q, a):
B guess = input(q + ' ')
C return guess = a
D print(ask('2+2', '4'))

Answer Key

Quiz program using a function, a loop and a score
Section A: Quick Questions
Q1
A — if ask(q, a): score += 1
Q2
A — return True or False
Q3
A — Before the loop starts
Q4
B — def ask(q, a): return input(q) == a
Q5
B — So the same function can ask any question
Section B: Match the Pairs
Q1
def ask(question, answer): → Defines a function that takes a question and its correct answer as parameters
Q2
return reply == answer → Gives back True if the player's reply matches the answer, otherwise False
Q3
Add 1 to the score each time the player gets a question right → if ask(q, a): score += 1
Q4
Show the final score after the loop ends using an f-string → print(f"You scored {score}")
Q5
if ask(q, a): score += 1 → score += 1 is not indented, so it is not inside the if block and Python raises an IndentationError.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def ask(question, answer): return input(question) == answer
Q2
def ask(q, a): return input(q) == a
Q3
for q, a in quiz: if ask(q, a): score += 1
Q4
score = 0 for q, a in quiz: if ask(q, a): score += 1
Q5
print(f"Score: {score}/{len(quiz)}")
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.