انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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).
  • zip walks 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:
5 + 7 = 12 ✓
9 x 3 = 26 ✗ the answer is 27
20 - 8 = 12 ✓
36 / 6 = 6 ✓
Score: 3 / 4
🐍 Open the code in Robo Python ↗
Marking and the score in Robo Python
Marking and the score in Robo Python

📌 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.

🐍 The game with a percentage
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:
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 %
🐍 Open the code in Robo Python ↗
✍️ Practise
Change the answer 26 to 27 and run. What is the score now?
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Only two questions, about your city and your favourite colour.

👀 Show the solution

When the program asks, we type: Cairo, then 4.

🐍 Solution
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:
Biggest city in Egypt? Cairo
2 + 2 = ? 4
Score: 2
🐍 Open the code in Robo Python ↗

Result: Score: 2

Medium

A message at the end depending on the result: excellent if they are all right.

👀 Show the solution

When the program asks, we type: cairo, then 30, then blue.

🐍 Solution
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:
Capital of Egypt? cairo
Correct!
5 x 6 = ? 30
Correct!
Colour of the sky? blue
Correct!
Excellent!
🐍 Open the code in Robo Python ↗

Result: Excellent!

Hard

The questions in a dictionary, in a random order (shuffle).

👀 Show the solution

When the program asks, we type: 5, then cairo, then blue.

🐍 Solution
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:
Sky colour? 5
Capital of Egypt? cairo
2 + 3 = ? blue
Score: 1
🐍 Open the code in Robo Python ↗

Result: the same questions in another order.

Finished the lesson? Mark it so your progress counts.