We build the game
Section 14: The final project: the quiz gameLesson 14.1 · ⏱ 14 min
🎯 Project goal
A maths quiz game: every question has a right answer, and we mark the player's answers and give a score.
- Every question is a pair (tuple) of the question text and the answer:
("5 + 7", 12). zipwalks through the two lists together: the question and the player's answer.- Here the player's answers are written in a list so we can test quickly, and in the next step we take them with input.
questions = [
("5 + 7", 12),
("9 x 3", 27),
("20 - 8", 12),
("36 / 6", 6),
]
answers = [12, 26, 12, 6] # the player's answers (try changing them)
score = 0
for (q, right), a in zip(questions, answers):
if a == right:
score += 1
print(q, "=", a, "✓")
else:
print(q, "=", a, "✗ the answer is", right)
print("Score:", score, "/", len(questions))
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗5 + 7 = 12 ✓ 9 x 3 = 26 ✗ the answer is 27 20 - 8 = 12 ✓ 36 / 6 = 6 ✓ Score: 3 / 4

📌 Another example: the game with a percentage
At the end we print the result as a percentage out of 100.
When the program asks, we type: Cairo, then 30, then green.
questions = [
("Capital of Egypt?", "cairo"),
("5 x 6 = ?", "30"),
("Colour of the sky?", "blue"),
]
score = 0
for q, a in questions:
if input(q + " ").strip().lower() == a:
print("Correct!")
score = score + 1
else:
print("No, it is", a)
print("Score:", score, "/", len(questions))
print("That is", round(score / len(questions) * 100), "%")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Capital of Egypt? Cairo Correct! 5 x 6 = ? 30 Correct! Colour of the sky? green No, it is blue Score: 2 / 3 That is 67 %
✍️ Practise
Change the answer 26 to 27 and run. What is the score now?
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Only two questions, about your city and your favourite colour.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: Cairo, then 4.
score = 0
if input("Biggest city in Egypt? ").lower() == "cairo":
score += 1
if input("2 + 2 = ? ") == "4":
score += 1
print("Score:", score)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Biggest city in Egypt? Cairo 2 + 2 = ? 4 Score: 2
Result: Score: 2
Medium
A message at the end depending on the result: excellent if they are all right.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: cairo, then 30, then blue.
questions = [
("Capital of Egypt?", "cairo"),
("5 x 6 = ?", "30"),
("Colour of the sky?", "blue"),
]
score = 0
for q, a in questions:
if input(q + " ").strip().lower() == a:
print("Correct!")
score = score + 1
else:
print("No, it is", a)
if score == len(questions):
print("Excellent!")
else:
print("Try again!")
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Capital of Egypt? cairo Correct! 5 x 6 = ? 30 Correct! Colour of the sky? blue Correct! Excellent!
Result: Excellent!
Hard
The questions in a dictionary, in a random order (shuffle).
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 5, then cairo, then blue.
import random
random.seed(4)
qs = {"2 + 3 = ?": "5", "Capital of Egypt?": "cairo", "Sky colour?": "blue"}
items = list(qs.items())
random.shuffle(items)
score = 0
for q, a in items:
if input(q + " ").lower() == a:
score += 1
print("Score:", score)
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Sky colour? 5 Capital of Egypt? cairo 2 + 3 = ? blue Score: 1
Result: the same questions in another order.

