Quiz Game
🧠 Quiz Game
🎮 What We're Building
A quiz that presents a series of multiple-choice questions, checks each answer as it's given, and prints a final score out of the total, plus a percentage.
Step 1: Representing Questions
Each question is a dict (Course 1, Chapter 7) with three keys; the whole quiz is a list of these dicts, reusing exactly the "list of dicts" shape Chapter 4's to-do app used for tasks. "answer" stores the correct choice's letter, not its full text — comparing letters is simpler than comparing whole sentences.
Step 2: Presenting a Question
enumerate (Course 1, Chapter 4) pairs each choice with its position; letters[i] reuses plain string indexing (Course 1, Chapter 5) to turn that position into the matching letter — letters[0] is "A", letters[1] is "B", and so on.
Step 3: Checking Answers & Scoring
A player typing "b", " B", or "B " should all count as correct — but "b" == "B" is False (Course 1's string comparisons are case-sensitive), and stray whitespace from pressing Enter carelessly breaks an exact match too. .strip() (Course 1, Chapter 5) removes surrounding whitespace, .upper() normalizes case — chaining both before comparing is standard practice for any free-text-ish user input.
Step 4: The Quiz Loop & Final Score
score is the accumulator pattern one more time (Course 1, Chapter 4) — incremented once per correct answer, read only after the loop finishes. score / len(questions) reuses the average-style division from earlier chapters, scaled to a percentage and formatted to a whole number with :.0f (Course 1, Chapter 5).
🏁 The Complete Quiz Game
List of dicts
Each question is a dict; the quiz is a list of them — same shape as Chapter 4's tasks.
enumerate + string indexing
Turns each choice's position into a letter label (A, B, C...).
Normalized comparison
.strip().upper() before comparing user input to the stored answer.
Scoring
An accumulator, converted to a percentage at the end.
Try these on your own:
- Use
random.shuffle(questions)so the question order is different each playthrough. - Load
questionsfrom an external JSON file (Course 2, Chapter 7) instead of hardcoding them, so new quizzes don't require editing the code. - Add a category field to each question, and let the player choose a category before starting.
- Track and display which specific questions were missed at the end, not just the final score.
🎯 What's Next
Next chapter: Contact Book — file I/O and CRUD operations against a JSON file.