⚡0 XP🔥0

Project 2: Quiz Game (OOP)

Lesson 33 of 33 ⏱ 5 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs Python

Build an object-oriented quiz game with Question and Quiz classes, scoring, shuffling, input validation, results summary and high scores saved to JSON.

What we'll build#

A command-line quiz game:

  • Multiple-choice questions with 4 options each
  • Questions shuffled every game
  • Input validation (no crash on wrong input)
  • Score, percentage and a review of wrong answers at the end
  • High scores saved to a JSON file

Concepts used: classes and objects, lists, random, loops, try/except, f-strings, and JSON files.

Step 1: Plan the classes#

Class Data (attributes) Behaviour (methods)
Question text, options, answer index is_correct(choice), display(number)
Quiz list of questions, score, wrong answers play(), ask(question), show_result()
ScoreBoard file name load(), save(name, score), top(n)
हिंदी में समझें

OOP project बनाने से पहले सोचें: "मेरे program में कौन-कौन सी चीज़ें (objects) हैं, और हर चीज़ क्या जानती है (data) और क्या कर सकती है (methods)?"

यहाँ तीन चीज़ें हैं:

  • Question - अपना सवाल, options और सही जवाब जानता है, और बता सकता है कि दिया गया जवाब सही है या नहीं।
  • Quiz - सारे सवालों को क्रम से पूछता है और score गिनता है।
  • ScoreBoard - high scores को file में save और load करता है।

हर class अपना एक काम करती है - इससे code साफ़ और आसानी से बदलने लायक रहता है।

Step 2: The Question class#

▶ Try it YourselfPython
Ctrl + Enter to run

Step 3: Asking safely (input validation)#

Players make typos. The game must not crash:

▶ Try it YourselfPython
Ctrl + Enter to run

Step 4: The complete game#

Click + Input, enter your name and then 5 answers (one per line), for example:

Asha
4
2
3
1
3
▶ Try it YourselfPython
Ctrl + Enter to run
Did you know?

random.seed(7) makes the shuffle the same every run so the sample inputs above match. Remove that line in your own version to get a different order each game.

Step 5: Ideas to extend the game#

  1. Load questions from JSON - keep the question bank in questions.json so non-programmers can add questions.
  2. Categories and difficulty - add category and level attributes; let the player choose.
  3. Timer - use time.time() to give a bonus for fast answers.
  4. Lifelines - "50-50" removes two wrong options (pick them with random.sample).
  5. Web version - later, turn it into a Flask web app with the same classes!

Summary#

You designed and built an OOP program:

  • Question encapsulates one question's data and checking logic.
  • Quiz manages the game flow and scoring.
  • ScoreBoard persists data in JSON.
  • Input validation keeps the program from crashing.

Congratulations on finishing the Python course! Next, continue with Prompt Engineering to learn how to work effectively with AI models.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

The Question class

Create class Question(text, options, answer) where answer is the index of the correct option. Add:

  • is_correct(choice) → True if choice (an index) equals the answer
  • correct_option() → the text of the correct option
2

The Quiz class

Using your Question class, create class Quiz(questions) with:

  • score starting at 0
  • answer(index, choice) → checks question number index; if correct add 1 to score; return True/False
  • percentage() → score as a percentage of the number of questions, rounded to 1 decimal

Quick Quiz

Q1.

Why do we create a separate Question class?

Q2.

Which module shuffles the questions?

Q3.

Where are high scores stored so they survive after the program closes?

Q4.

How does the program handle a player typing "x" instead of an option number?