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#
Step 3: Asking safely (input validation)#
Players make typos. The game must not crash:
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
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#
- Load questions from JSON - keep the question bank in
questions.jsonso non-programmers can add questions. - Categories and difficulty - add
categoryandlevelattributes; let the player choose. - Timer - use
time.time()to give a bonus for fast answers. - Lifelines - "50-50" removes two wrong options (pick them with
random.sample). - 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.