Project: Quiz Game

What you'll learn
- How to store program data as a list of dictionaries instead of hard-coding it in
ifstatements - How to split a program into small functions, each with one clear job
- How to separate pure functions (easy to test) from functions that use
input()andprint() - How to shuffle without changing the original data, and how to score results per topic
- How to check your logic with simple
asserttests
Why it matters
Imagine writing a quiz with ten if statements, one per question. Adding a question means copying code, and changing the scoring means editing ten places. Real programs do it the other way around: the data (questions) lives in one structure, and a few functions work on any amount of that data.
This project uses everything from the last two modules: lists, dictionaries, sets, comprehensions, and functions with parameters and return values. It is the same design as a driving-theory test, a school exam app, or a language-learning app: questions in, results out.
Goal and features
The program asks multiple-choice questions in random order, checks the answers, and prints a report like this:
=== Results ===
Score: 6/9 (66.7%) Grade: C
By topic:
Geography 3/3 100.0%
Math 1/3 33.3%
Python 2/3 66.7%
Review your mistakes:
- What is 2 ** 10?
You chose: 512. Correct: 1024.Features:
| Feature | Tools you will use |
|---|---|
| Questions with options, answer, and topic | a list of dictionaries |
| Display a question with A-D options | zip(), str.join() |
Validate answers like b, B, or E | string methods, a while loop |
| Random order of questions and options | random.sample() |
| Score per topic | a dictionary of dictionaries, setdefault() |
| Percentage and grade | small pure functions |
| Review wrong answers | a list comprehension |
| Check the logic | assert tests |
Design
Each function has one job. Most of them are pure: they receive data and return a result, without asking or printing anything. Only three functions talk to the player.
QUESTIONSlist of dicts
run_quiz(questions)talks to the player
shuffled(items)new list in random order- for each question:
ask_question(...)talks to the playerformat_question(...)purestrletter_to_option(...)pureoption orNone
check_answer(question, chosen)pureboolresults.append({...})
show_results(results)prints the report
score_by_topic(results)puredictpercentage(correct, total)purefloatgrade(percent)purestr
The results list is the bridge between asking and reporting. Each item is a dictionary that remembers the question, the topic, what the player chose, the correct answer, and whether it was correct.
Step 1: Store the questions as data
Every question has the same four keys. That uniform shape is what lets one function handle all of them:
QUESTIONS = [
{"question": "What is the capital of Uzbekistan?",
"options": ["Samarkand", "Tashkent", "Bukhara", "Khiva"],
"answer": "Tashkent", "topic": "Geography"},
{"question": "Which type is immutable?",
"options": ["list", "dict", "set", "tuple"],
"answer": "tuple", "topic": "Python"},
{"question": "What is 2 ** 10?",
"options": ["20", "100", "512", "1024"],
"answer": "1024", "topic": "Math"},
]
print("Questions:", len(QUESTIONS))
print("Topics:", sorted({q["topic"] for q in QUESTIONS}))
print("First question:", QUESTIONS[0]["question"])
print("Its options:", QUESTIONS[0]["options"])
# every answer must be one of its own options
print("All answers valid:", all(q["answer"] in q["options"] for q in QUESTIONS))Questions: 3
Topics: ['Geography', 'Math', 'Python']
First question: What is the capital of Uzbekistan?
Its options: ['Samarkand', 'Tashkent', 'Bukhara', 'Khiva']
All answers valid: TrueThe set comprehension {q["topic"] for q in QUESTIONS} collects each topic once. The last line is a quick data check: a typo such as "answer": "Tashknet" would make a question impossible to answer correctly, and all() catches it before the quiz starts.
Step 2: Show a question
format_question returns a string instead of printing it. That makes it easy to test and reuse (for example, to save a quiz to a file later).
LETTERS = "ABCD"
def format_question(number, total, question, options):
"""Return the question and its lettered options as one string."""
lines = [f"Question {number}/{total} [{question['topic']}]",
question["question"]]
for letter, option in zip(LETTERS, options):
lines.append(f" {letter}) {option}")
return "\n".join(lines)
question = {"question": "What is the capital of Uzbekistan?",
"options": ["Samarkand", "Tashkent", "Bukhara", "Khiva"],
"answer": "Tashkent", "topic": "Geography"}
text = format_question(1, 9, question, question["options"])
print(text)Question 1/9 [Geography]
What is the capital of Uzbekistan?
A) Samarkand
B) Tashkent
C) Bukhara
D) Khivazip(LETTERS, options) pairs "A" with the first option, "B" with the second, and so on. The options are passed as a separate parameter, not read from the dictionary, because in Step 5 you will show them in shuffled order.
Step 3: Turn a letter into an answer and check it
The player types a letter, but the answer is stored as text. One function translates, another compares:
LETTERS = "ABCD"
def letter_to_option(letter, options):
"""Turn 'b' or 'B' into the matching option, or None if invalid."""
letter = letter.strip().upper()
if len(letter) != 1 or letter not in LETTERS[:len(options)]:
return None
return options[LETTERS.index(letter)]
def check_answer(question, chosen):
return chosen == question["answer"]
question = {"question": "What is the capital of Uzbekistan?",
"options": ["Samarkand", "Tashkent", "Bukhara", "Khiva"],
"answer": "Tashkent", "topic": "Geography"}
options = question["options"]
for reply in ["B", " b ", "A", "E", "AB", ""]:
label = '"' + reply + '"'
chosen = letter_to_option(reply, options)
if chosen is None:
print(f"{label:6} -> invalid")
else:
print(f"{label:6} -> {chosen:<10} correct: {check_answer(question, chosen)}")"B" -> Tashkent correct: True
" b " -> Tashkent correct: True
"A" -> Samarkand correct: False
"E" -> invalid
"AB" -> invalid
"" -> invalidWhy check len(letter) != 1? Because "" in "ABCD" is True: the empty string is inside every string. Without that check, pressing Enter would silently choose option A. LETTERS[:len(options)] also makes the function work for questions with only two or three options.
letter_to_option returns None for anything invalid. Returning a special "no result" value is a common pattern you met in Return Values.
Step 4: Ask the player
Now the first function that uses input(). It shows the question and keeps asking until it gets a valid letter or quit:
LETTERS = "ABCD"
def format_question(number, total, question, options):
lines = [f"Question {number}/{total} [{question['topic']}]",
question["question"]]
for letter, option in zip(LETTERS, options):
lines.append(f" {letter}) {option}")
return "\n".join(lines)
def letter_to_option(letter, options):
letter = letter.strip().upper()
if len(letter) != 1 or letter not in LETTERS[:len(options)]:
return None
return options[LETTERS.index(letter)]
def ask_question(number, total, question, options):
"""Ask until the player types a valid letter. Return the option or None to quit."""
print(format_question(number, total, question, options))
while True:
reply = input("Your answer (A-D, or quit): ").strip()
if reply.lower() == "quit":
return None
chosen = letter_to_option(reply, options)
if chosen is not None:
return chosen
print(" Please type A, B, C, or D.")
question = {"question": "Which type is immutable?",
"options": ["list", "dict", "set", "tuple"],
"answer": "tuple", "topic": "Python"}
chosen = ask_question(1, 1, question, question["options"])
print("Returned:", chosen)Question 1/1 [Python]
Which type is immutable?
A) list
B) dict
C) set
D) tuple
Your answer (A-D, or quit): x
Please type A, B, C, or D.
Your answer (A-D, or quit): D
Returned: tupleThe return inside the loop ends both the loop and the function. Notice how little logic this function has: formatting and validation are delegated to the pure functions from Steps 2 and 3.
Step 5: Shuffle without changing the data
random.shuffle(items) shuffles a list in place and returns None. That would reorder the global QUESTIONS list, which is a side effect you do not want. random.sample(items, len(items)) returns a new shuffled list instead. (You will study the random module properly in math, random, and statistics.)
import random
random.seed(3)
def shuffled(items):
"""Return a new list in random order. The original list is not changed."""
return random.sample(items, len(items))
options = ["Samarkand", "Tashkent", "Bukhara", "Khiva"]
print(shuffled(options))
print(shuffled(options))
print("Original:", options)['Tashkent', 'Bukhara', 'Samarkand', 'Khiva']
['Khiva', 'Bukhara', 'Samarkand', 'Tashkent']
Original: ['Samarkand', 'Tashkent', 'Bukhara', 'Khiva']random.seed(3) makes the order repeatable, so the outputs in this lesson are real. Delete it in your own quiz so every game is different.
Step 6: Score by topic, percentage, and grade
After the quiz, results holds one dictionary per answer. score_by_topic groups them:
def score_by_topic(results):
"""Return {topic: {"correct": n, "total": n}}."""
scores = {}
for result in results:
topic_score = scores.setdefault(result["topic"], {"correct": 0, "total": 0})
topic_score["total"] += 1
if result["correct"]:
topic_score["correct"] += 1
return scores
def percentage(correct, total):
if total == 0:
return 0.0
return round(correct / total * 100, 1)
def grade(percent):
if percent >= 90:
return "A"
if percent >= 75:
return "B"
if percent >= 60:
return "C"
if percent >= 40:
return "D"
return "F"
results = [
{"topic": "Geography", "correct": True},
{"topic": "Python", "correct": True},
{"topic": "Math", "correct": False},
{"topic": "Python", "correct": False},
{"topic": "Geography", "correct": True},
]
print(score_by_topic(results))
correct = sum(1 for r in results if r["correct"])
percent = percentage(correct, len(results))
print(f"{correct}/{len(results)} = {percent}% -> grade {grade(percent)}")
print(percentage(0, 0), grade(100), grade(59.9)){'Geography': {'correct': 2, 'total': 2}, 'Python': {'correct': 1, 'total': 2}, 'Math': {'correct': 0, 'total': 1}}
3/5 = 60.0% -> grade C
0.0 A Dscores.setdefault(topic, {"correct": 0, "total": 0}) returns the existing inner dictionary, or creates it the first time a topic appears. Because dictionaries are mutable, topic_score["total"] += 1 updates the dictionary stored inside scores.
percentage guards against dividing by zero, which happens if the player quits before answering anything. grade checks the highest threshold first, and each return ends the function, so no elif is needed.
Step 7: Review the wrong answers
A list comprehension picks the mistakes, and a loop prints them:
results = [
{"question": "What is 2 ** 10?", "topic": "Math",
"chosen": "512", "answer": "1024", "correct": False},
{"question": "Which type is immutable?", "topic": "Python",
"chosen": "tuple", "answer": "tuple", "correct": True},
{"question": "What is 17 // 5?", "topic": "Math",
"chosen": "3.4", "answer": "3", "correct": False},
]
wrong = [r for r in results if not r["correct"]]
print(f"Review your mistakes ({len(wrong)}):")
for r in wrong:
print(f" - {r['question']}")
print(f" You chose: {r['chosen']}. Correct: {r['answer']}.")Review your mistakes (2):
- What is 2 ** 10?
You chose: 512. Correct: 1024.
- What is 17 // 5?
You chose: 3.4. Correct: 3.Storing chosen and answer in each result is what makes this review possible. When you design the results, think about what the report will need.
The full program
Here is everything in one file. Copy it into quiz.py and run it.
import random
random.seed(3) # same order every run while testing; delete this line for real quizzes
# ---------- Step 1: the questions ----------
QUESTIONS = [
{"question": "What is the capital of Uzbekistan?",
"options": ["Samarkand", "Tashkent", "Bukhara", "Khiva"],
"answer": "Tashkent", "topic": "Geography"},
{"question": "Which city is famous for the Registan square?",
"options": ["Samarkand", "Namangan", "Nukus", "Termez"],
"answer": "Samarkand", "topic": "Geography"},
{"question": "Which sea is shared by Uzbekistan and Kazakhstan?",
"options": ["Black Sea", "Caspian Sea", "Aral Sea", "Red Sea"],
"answer": "Aral Sea", "topic": "Geography"},
{"question": "What does len([3, 1, 2]) return?",
"options": ["2", "3", "6", "[1, 2, 3]"],
"answer": "3", "topic": "Python"},
{"question": "Which type is immutable?",
"options": ["list", "dict", "set", "tuple"],
"answer": "tuple", "topic": "Python"},
{"question": "What does a function return without a return statement?",
"options": ["0", "None", "False", "an empty string"],
"answer": "None", "topic": "Python"},
{"question": "What is 15% of 80,000 som?",
"options": ["8,000 som", "12,000 som", "15,000 som", "1,200 som"],
"answer": "12,000 som", "topic": "Math"},
{"question": "What is 2 ** 10?",
"options": ["20", "100", "512", "1024"],
"answer": "1024", "topic": "Math"},
{"question": "What is 17 // 5?",
"options": ["3", "3.4", "2", "4"],
"answer": "3", "topic": "Math"},
]
LETTERS = "ABCD"
# ---------- Step 2: show a question ----------
def format_question(number, total, question, options):
"""Return the question and its lettered options as one string."""
lines = [f"Question {number}/{total} [{question['topic']}]",
question["question"]]
for letter, option in zip(LETTERS, options):
lines.append(f" {letter}) {option}")
return "\n".join(lines)
# ---------- Step 3: check an answer ----------
def letter_to_option(letter, options):
"""Turn 'b' or 'B' into the matching option, or None if invalid."""
letter = letter.strip().upper()
if len(letter) != 1 or letter not in LETTERS[:len(options)]:
return None
return options[LETTERS.index(letter)]
def check_answer(question, chosen):
return chosen == question["answer"]
# ---------- Step 4: ask the player ----------
def ask_question(number, total, question, options):
"""Ask until the player types a valid letter. Return the option or None to quit."""
print(format_question(number, total, question, options))
while True:
reply = input("Your answer (A-D, or quit): ").strip()
if reply.lower() == "quit":
return None
chosen = letter_to_option(reply, options)
if chosen is not None:
return chosen
print(" Please type A, B, C, or D.")
# ---------- Step 5: shuffling ----------
def shuffled(items):
"""Return a new list in random order. The original list is not changed."""
return random.sample(items, len(items))
# ---------- Step 6: scoring ----------
def score_by_topic(results):
"""Return {topic: {"correct": n, "total": n}}."""
scores = {}
for result in results:
topic_score = scores.setdefault(result["topic"], {"correct": 0, "total": 0})
topic_score["total"] += 1
if result["correct"]:
topic_score["correct"] += 1
return scores
def percentage(correct, total):
if total == 0:
return 0.0
return round(correct / total * 100, 1)
def grade(percent):
if percent >= 90:
return "A"
if percent >= 75:
return "B"
if percent >= 60:
return "C"
if percent >= 40:
return "D"
return "F"
# ---------- Step 7: results and review ----------
def show_results(results):
if not results:
print("\nNo questions answered.")
return
correct = sum(1 for r in results if r["correct"])
total = len(results)
percent = percentage(correct, total)
print("\n=== Results ===")
print(f"Score: {correct}/{total} ({percent}%) Grade: {grade(percent)}")
print("\nBy topic:")
for topic, s in sorted(score_by_topic(results).items()):
topic_percent = percentage(s["correct"], s["total"])
print(f" {topic:<10} {s['correct']}/{s['total']} {topic_percent:>5}%")
wrong = [r for r in results if not r["correct"]]
if wrong:
print("\nReview your mistakes:")
for r in wrong:
print(f" - {r['question']}")
print(f" You chose: {r['chosen']}. Correct: {r['answer']}.")
else:
print("\nPerfect score. Well done!")
def run_quiz(questions):
results = []
order = shuffled(questions)
for number, question in enumerate(order, start=1):
options = shuffled(question["options"])
print()
chosen = ask_question(number, len(order), question, options)
if chosen is None:
print("Quiz stopped early.")
break
is_correct = check_answer(question, chosen)
print(" Correct!" if is_correct else f" Wrong. The answer is {question['answer']}.")
results.append({
"question": question["question"],
"topic": question["topic"],
"chosen": chosen,
"answer": question["answer"],
"correct": is_correct,
})
show_results(results)
print("=== Uzbekistan and Python Quiz ===")
run_quiz(QUESTIONS)A real session. The player types an invalid letter once and makes three mistakes:
=== Uzbekistan and Python Quiz ===
Question 1/9 [Python]
What does len([3, 1, 2]) return?
A) 6
B) [1, 2, 3]
C) 2
D) 3
Your answer (A-D, or quit): D
Correct!
Question 2/9 [Geography]
Which sea is shared by Uzbekistan and Kazakhstan?
A) Red Sea
B) Aral Sea
C) Caspian Sea
D) Black Sea
Your answer (A-D, or quit): e
Please type A, B, C, or D.
Your answer (A-D, or quit): B
Correct!
Question 3/9 [Math]
What is 2 ** 10?
A) 100
B) 20
C) 512
D) 1024
Your answer (A-D, or quit): C
Wrong. The answer is 1024.
Question 4/9 [Python]
Which type is immutable?
A) list
B) set
C) tuple
D) dict
Your answer (A-D, or quit): C
Correct!
Question 5/9 [Math]
What is 17 // 5?
A) 3
B) 3.4
C) 4
D) 2
Your answer (A-D, or quit): B
Wrong. The answer is 3.
Question 6/9 [Geography]
What is the capital of Uzbekistan?
A) Khiva
B) Bukhara
C) Tashkent
D) Samarkand
Your answer (A-D, or quit): C
Correct!
Question 7/9 [Math]
What is 15% of 80,000 som?
A) 1,200 som
B) 15,000 som
C) 12,000 som
D) 8,000 som
Your answer (A-D, or quit): C
Correct!
Question 8/9 [Python]
What does a function return without a return statement?
A) False
B) 0
C) an empty string
D) None
Your answer (A-D, or quit): B
Wrong. The answer is None.
Question 9/9 [Geography]
Which city is famous for the Registan square?
A) Termez
B) Samarkand
C) Namangan
D) Nukus
Your answer (A-D, or quit): B
Correct!
=== Results ===
Score: 6/9 (66.7%) Grade: C
By topic:
Geography 3/3 100.0%
Math 1/3 33.3%
Python 2/3 66.7%
Review your mistakes:
- What is 2 ** 10?
You chose: 512. Correct: 1024.
- What is 17 // 5?
You chose: 3.4. Correct: 3.
- What does a function return without a return statement?
You chose: 0. Correct: None.Walk through the output:
ewas rejected, and question 2 was asked again without counting anything.- 6 correct out of 9 is 66.7%, which is at least 60, so the grade is C.
- Every topic appears in the report, sorted by name, because
sorted()on.items()sorts by key. - The review lists exactly the three
resultswherecorrectisFalse.
Testing the pure functions
Pure functions are easy to test: call them with known input and assert the result. In a real project you would keep these tests in a separate file. Here the functions are repeated so the block runs on its own:
LETTERS = "ABCD"
def letter_to_option(letter, options):
letter = letter.strip().upper()
if len(letter) != 1 or letter not in LETTERS[:len(options)]:
return None
return options[LETTERS.index(letter)]
def check_answer(question, chosen):
return chosen == question["answer"]
def score_by_topic(results):
scores = {}
for result in results:
topic_score = scores.setdefault(result["topic"], {"correct": 0, "total": 0})
topic_score["total"] += 1
if result["correct"]:
topic_score["correct"] += 1
return scores
def percentage(correct, total):
if total == 0:
return 0.0
return round(correct / total * 100, 1)
def grade(percent):
if percent >= 90:
return "A"
if percent >= 75:
return "B"
if percent >= 60:
return "C"
if percent >= 40:
return "D"
return "F"
def test_letter_to_option():
options = ["list", "dict", "set", "tuple"]
assert letter_to_option("A", options) == "list"
assert letter_to_option(" d ", options) == "tuple"
assert letter_to_option("E", options) is None
assert letter_to_option("", options) is None
assert letter_to_option("AB", options) is None
assert letter_to_option("C", ["yes", "no"]) is None # only A and B exist
def test_check_answer():
question = {"answer": "tuple"}
assert check_answer(question, "tuple")
assert not check_answer(question, "list")
def test_score_by_topic():
results = [
{"topic": "Math", "correct": True},
{"topic": "Math", "correct": False},
{"topic": "Python", "correct": True},
]
assert score_by_topic(results) == {
"Math": {"correct": 1, "total": 2},
"Python": {"correct": 1, "total": 1},
}
assert score_by_topic([]) == {}
def test_percentage():
assert percentage(3, 4) == 75.0
assert percentage(2, 3) == 66.7
assert percentage(0, 0) == 0.0
def test_grade_boundaries():
assert grade(100) == "A"
assert grade(90) == "A"
assert grade(89.9) == "B"
assert grade(60) == "C"
assert grade(40) == "D"
assert grade(0) == "F"
tests = [test_letter_to_option, test_check_answer, test_score_by_topic,
test_percentage, test_grade_boundaries]
for test in tests:
test()
print("passed:", test.__name__)passed: test_letter_to_option
passed: test_check_answer
passed: test_score_by_topic
passed: test_percentage
passed: test_grade_boundariesFunctions are values, so the loop can call each test from the list. Every function also knows its own name: test.__name__ is the text you wrote after def, which is why each line shows which test passed.
Good tests cover normal cases, edge cases (empty input, 0 / 0), and boundaries: grade(90) and grade(89.9) check exactly where A turns into B. If you later change a threshold by mistake, a test fails immediately.
Try it
A compact quiz with three questions and a random order on every run. Run it and type your answers (A, B, C, or D) as the questions appear. Then add your own question to the list.
Common mistakes
1. Using the result of random.shuffle()
import random
questions = ["q1", "q2", "q3"]
order = random.shuffle(questions)
for question in order:
print(question)TypeError: 'NoneType' object is not iterableshuffle() changes the list in place and returns None. Fix: use order = random.sample(questions, len(questions)), which returns a new list and leaves the original alone.
2. Comparing the letter with the answer
question = {"options": ["Samarkand", "Tashkent"], "answer": "Tashkent"}
reply = "B"
print(reply == question["answer"]) # Bad: "B" vs "Tashkent"
print(question["options"][1] == question["answer"]) # Good: compare option textFalse
TrueThe player can never score. Fix: convert the letter into the option first (letter_to_option), then compare text with text.
3. Accepting an empty answer
LETTERS = "ABCD"
reply = "" # the player just pressed Enter
print(reply in LETTERS) # Bad: looks valid
print(LETTERS.index(reply)) # and silently means option ATrue
0Fix: check len(letter) == 1 as well as letter in LETTERS, and add a test for "".
4. Dividing by zero when nobody answered
results = [] # the player typed quit at the first question
correct = sum(1 for r in results if r["correct"])
percent = correct / len(results) * 100ZeroDivisionError: division by zeroFix: guard the division (if total == 0: return 0.0) and handle an empty results list before printing the report.
Challenges
Try each one before opening the hints.
1. More questions and a new topic
Add three questions about History (for example, Amir Temur or the Silk Road). The report should show the new topic without any other code changes.
Hints
- Only
QUESTIONSneeds to change. If you had to edit a function, the design is not data-driven enough. - Run the
all(q["answer"] in q["options"] ...)check from Step 1 after adding them.
2. Choose the number of questions
Ask the player how many questions they want (from 1 to the number available), and ask only that many.
Hints
random.sample(questions, k)returnskrandom items, soshuffledcan take acountparameter.- Validate the number with
isdecimal()and a range check before converting, as in Project: Number Guessing Game.
3. Pick a topic
Before the quiz starts, show the available topics and let the player choose one, or all.
Hints
- Build the menu from
sorted({q["topic"] for q in QUESTIONS}), so new topics appear automatically. - Write a pure function
filter_by_topic(questions, topic)that returns a new list with a comprehension, and test it withassert.
4. Points per question
Give each question a "points" key (1 to 3, depending on difficulty). Show the score as points earned out of points possible, and per topic too.
Hints
- Store
pointsin each result dictionary. - Change
score_by_topicto addpointsinstead of1. Update its test first, then the function. - Use
q.get("points", 1)so old questions without the key still work.
5. Retry the mistakes
After the report, offer to repeat only the questions the player got wrong, and keep going until everything is correct or the player types quit. At the end, show how many rounds it took.
Hints
- Let
run_quizreturn theresultslist as well as printing the report. - Build the next round with a comprehension: the original question dictionaries whose result was wrong. Store the whole question in each result to make this easy.
- Loop
while wrong_questions:and count the rounds.
Recap
- Keep data (questions) separate from logic (functions). One list of dictionaries with the same keys can drive the whole program.
- Give every function one job.
format_question,letter_to_option,check_answer,score_by_topic,percentage, andgradeare pure. - Keep
input()andprint()in a few outer functions, so the rest can be tested withassert. random.sample(items, len(items))returns a shuffled copy.random.shuffle()changes the list and returnsNone.setdefault()builds nested counters, such as correct and total answers per topic.- Guard edge cases (an empty answer, an empty results list) and test boundaries, like the exact percentage where one grade turns into another.