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.
- Replace the answers list with
int(input(q + " = ")), and protect it with try so it doesn't stop if the player types a letter. - Make the questions with random: two numbers from 1 to 10 and a random operation from +, - and x.
- Keep each player's best score in a dictionary, and print the champions board.
- Make a
Playerclass with a name and a score, and anadd_pointfunction. - 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.
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:
🐍 Open the code in Robo Python ↗7 x 8 = ? 56 Correct! You took 1.2 seconds
✍️ Practise
Start your game in the lab, and save it with the save button to finish it later.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Show the question number: Question 1 of 3.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 4, then 9, then 3.
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:
🐍 Open the code 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
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🙈 Hide the solution
When the program asks, we type: rome, then paris.
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:
🐍 Open the code in Robo Python ↗Capital of France? rome Try again: paris Correct!
Result: Correct! on the second try.
Hard
Keep the highest score between two players in a dictionary.
👀 Show the solution🙈 Hide the solution
When the program asks, we type: 42, then 16, then 42, then 17.
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:
🐍 Open the code 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: MonaResult: Winner: Mona

