انتقل للمحتوى
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

Make it better, like the pros

Section 14: The final project: the quiz gameLesson 14.2 · ⏱ 15 min

Put everything you learned into your game. Pick one challenge, finish it, then move to the next.

  1. Replace the answers list with int(input(q + " = ")), and protect it with try so it doesn't stop if the player types a letter.
  2. Make the questions with random: two numbers from 1 to 10 and a random operation from +, - and x.
  3. Keep each player's best score in a dictionary, and print the champions board.
  4. Make a Player class with a name and a score, and an add_point function.
  5. At the end draw the players' scores as bars with matplotlib.
💡 Tip
Build one step, run it, make sure it works, then add the next step. That is how pro programmers work.

📌 Another example: a simple timer

We work out how many seconds the player took (time).

When the program asks, we type: 56.

🐍 A simple timer
import time
start = time.time()
a = input("7 x 8 = ? ")
seconds = round(time.time() - start, 1)
print("Correct!" if a == "56" else "No, it is 56")
print("You took", seconds, "seconds")
🖥️ The output in Robo Python:
7 x 8 = ? 56
Correct!
You took 1.2 seconds
🐍 Open the code in Robo Python ↗
✍️ Practise
Start your game in the lab, and save it with the save button to finish it later.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Show the question number: Question 1 of 3.

👀 Show the solution

When the program asks, we type: 4, then 9, then 3.

🐍 Solution
questions = [("2 + 2 = ?", "4"), ("3 x 3 = ?", "9"), ("10 - 7 = ?", "3")]
for i in range(len(questions)):
    q, a = questions[i]
    print("Question", i + 1, "of", len(questions))
    input(q + " ")
🖥️ The output in Robo Python:
Question 1 of 3
2 + 2 = ? 4
Question 2 of 3
3 x 3 = ? 9
Question 3 of 3
10 - 7 = ? 3
🐍 Open the code in Robo Python ↗

Result: Question 1 of 3 … Question 3 of 3

Medium

A second chance: if they get it wrong they try again before the answer appears.

👀 Show the solution

When the program asks, we type: rome, then paris.

🐍 Solution
a = input("Capital of France? ").lower()
if a != "paris":
    a = input("Try again: ").lower()
print("Correct!" if a == "paris" else "It is Paris")
🖥️ The output in Robo Python:
Capital of France? rome
Try again: paris
Correct!
🐍 Open the code in Robo Python ↗

Result: Correct! on the second try.

Hard

Keep the highest score between two players in a dictionary.

👀 Show the solution

When the program asks, we type: 42, then 16, then 42, then 17.

🐍 Solution
scores = {}
for player in ["Ali", "Mona"]:
    s = 0
    if input(player + ": 6 x 7 = ? ") == "42":
        s += 1
    if input(player + ": 9 + 8 = ? ") == "17":
        s += 1
    scores[player] = s
best = max(scores, key=scores.get)
print(scores)
print("Winner:", best)
🖥️ The output in Robo Python:
Ali: 6 x 7 = ? 42
Ali: 9 + 8 = ? 16
Mona: 6 x 7 = ? 42
Mona: 9 + 8 = ? 17
{'Ali': 1, 'Mona': 2}
Winner: Mona
🐍 Open the code in Robo Python ↗

Result: Winner: Mona

Finished the lesson? Mark it so your progress counts.