Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Projects
  3. Build a Quiz Game in Python

Project 3 · Guided build

Build a Quiz Game in Python

Create a scored multiple-choice quiz with a question bank, answer checking, explanations, and a final report.

Open the compilerWarm-up exercises
beginner60–90 minuteslists of dictionariesfunctionsloopsstring normalisation
On this page
  1. What you will build
  2. Question bank
  3. Core loop
  4. Validation
  5. Final report
  6. Structure the program
  7. Stretch goals
  8. Testing checklist

A quiz game is a data-driven program: questions live in a structure, and the same logic processes every one. You will build a question bank, score the answers, and report results.

What you will build#

A console quiz that:

  • Stores at least five questions in a list.
  • Prints each question with lettered options.
  • Accepts A, B, C, or D in either case.
  • Reports whether each answer is correct.
  • Tracks the score.
  • Prints a final summary with a percentage.

Question bank#

Each question is a dictionary with a prompt, options, the correct index, and an explanation:

Python
questions = [
    {
        "prompt": "Which keyword defines a function?",
        "options": ["def", "func", "function", "lambda"],
        "answer": 0,
        "explanation": "Functions are defined with def.",
    },
    {
        "prompt": "What does len([1, 2, 3]) return?",
        "options": ["2", "3", "4", "An error"],
        "answer": 1,
        "explanation": "The list contains three items.",
    },
]

Using zero-based indexes keeps the data simple. When you display options, convert the index to a letter with chr(ord("A") + index).

Core loop#

Python
score = 0

for number, question in enumerate(questions, start=1):
    print(f"\\nQuestion {number}: {question['prompt']}")

    for index, option in enumerate(question["options"]):
        letter = chr(ord("A") + index)
        print(f"  {letter}. {option}")

    answer = input("Your answer: ").strip().upper()
    correct_letter = chr(ord("A") + question["answer"])

    if answer == correct_letter:
        print("Correct!")
        score += 1
    else:
        print(f"Not quite. The answer is {correct_letter}.")

    print(question["explanation"])

Validation#

The basic loop accepts any text and treats it as wrong. That is acceptable for a first version, but a better quiz does not penalise a typo. Add a loop that asks again until the answer is one of the valid letters:

Python
valid = "ABCD"[: len(question["options"])]

while True:
    answer = input("Your answer: ").strip().upper()
    if answer in valid:
        break
    print(f"Choose one of {valid}.")

With stdin supplied in advance, an invalid answer consumes a line and repeats the prompt. Prepare enough input lines for the retries.

Final report#

Python
total = len(questions)
percentage = score / total * 100

print(f"\\nScore: {score}/{total} ({percentage:.0f}%)")

if percentage == 100:
    print("Perfect score!")
elif percentage >= 70:
    print("Good work.")
else:
    print("Review the explanations and try again.")

Guard against division by zero if you allow an empty question bank. Either require at least one question or handle the empty case explicitly.

Structure the program#

Move the quiz into functions:

  • ask_question(question, number) returns True or False.
  • run_quiz(questions) returns the score.
  • print_report(score, total) prints the summary.

This makes each part testable. You can later replace console input with another source without rewriting the scoring logic.

Shuffle carefully

Use random.shuffle(options) only if you also update the correct answer index. A simpler approach is to shuffle the question order and keep each option order fixed.

Stretch goals#

  • Add categories and let the player choose one.
  • Keep a best score in a variable for repeated rounds.
  • Add a time limit using time.time().
  • Print a per-category breakdown.
  • Load questions from JSON text with json.loads() and validate the structure.

Testing checklist#

  • Answer every question correctly and confirm 100%.
  • Answer every question incorrectly and confirm 0%.
  • Enter lowercase letters.
  • Enter an invalid letter and verify the retry prompt.
  • Confirm the correct letter matches the intended option.
  • Check the empty-bank behaviour.
Previous projectBuild a Command-Line Todo List in PythonNext projectBuild a Contact Book in Python

On this page

  1. What you will build
  2. Question bank
  3. Core loop
  4. Validation
  5. Final report
  6. Structure the program
  7. Stretch goals
  8. Testing checklist

Project checklist

  • Read the requirements.
  • Plan the data model.
  • Build one feature at a time.
  • Test invalid input.
  • Refactor after it works.