Lesson 12 of 35Python
Python · Lesson 12 · 45 minutes
Project: quiz game
What you'll learn
- Organise data as a list of dictionaries
- Compare answers tolerantly and count points
- Split the program into functions
Organising data well
A quiz consists of many questions, and every question has several parts: the question text and the right answer. A list of dictionaries is perfect for that:
questions = [
{"question": "Largest planet?", "answer": "Jupiter"},
{"question": "3 * 7?", "answer": "21"},
]
print(len(questions))
print(questions[0]["question"])
for q in questions:
print(q["question"], "→", q["answer"])New questions simply go into the list as another dictionary – the rest of the program stays the same.
Comparing tolerantly
“paris”, “Paris ” and “PARIS” should all count as correct. A function of its own keeps the program tidy – and you can test it separately:
def same(a, b):
return a.strip().lower() == b.strip().lower()
print(same(" Paris", "paris"))
print(same("Paris", "Berlin"))Shuffle and keep building
With random.shuffle() the questions come in a different order every time:
import random
questions = ["A", "B", "C", "D"]
random.shuffle(questions)
print(questions)Keep building: Add timing (
import time,time.time()), give bonus points for streaks or save the high score in a file – like in the lesson about files.
Your task
Your turn
Program a quiz. The questions are in the list questions. Ask each question with input(). If the answer is right (upper/lower case and spaces at the edges don't matter), print Correct!, otherwise Wrong – the answer is: Paris (with the real answer). At the end print Score: 2 of 3. For that, write a function check(answer, solution) that returns True or False.
questions = [
{"question": "Capital of France?", "answer": "Paris"},
{"question": "How many legs does a spider have?", "answer": "8"},
{"question": "Which language are you learning?", "answer": "Python"},
]
def check(answer, solution):
pass
# Your quiz- Something to think about
Compare
answer.strip().lower()withsolution.lower()and give the result back withreturn. - Which command helps?
Go through the list questions with
forand count the points.For wrong answers:
print(f"Wrong – the answer is: {q['answer']}")Don't hard-code the number of questions – use
len(questions). - Gap code – fill in the ___
questions = [ {"question": "Capital of France?", "answer": "Paris"}, {"question": "How many legs does a spider have?", "answer": "8"}, {"question": "Which language are you learning?", "answer": "Python"}, ] def check(answer, solution): return ___ points = ___ for q in ___: answer = ___ if ___: print(___) points += ___ else: print(___) print(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
questions = [
{"question": "Capital of France?", "answer": "Paris"},
{"question": "How many legs does a spider have?", "answer": "8"},
{"question": "Which language are you learning?", "answer": "Python"},
]
def check(answer, solution):
return answer.strip().lower() == solution.lower()
points = 0
for q in questions:
answer = input(q["question"] + " ")
if check(answer, q["answer"]):
print("Correct!")
points += 1
else:
print(f"Wrong – the answer is: {q['answer']}")
print(f"Score: {points} of {len(questions)}")