Create your own multiple-choice quiz that asks questions, checks answers and gives the player a final score.
Quizzes are everywhere: on TV game shows, in apps, and in classrooms. In this project you will build a quiz game about Python itself. The program asks questions one at a time, shows three possible answers, checks what the player picks, and adds up their score.
The clever part is how we store the questions. Each question is a dictionary, which is a way of labelling information. One dictionary holds the question text, the list of options, and the correct answer, all under handy names like "question" and "answer". Then we put all the dictionaries into one big list.
Because the questions are stored as data, you can change the whole quiz without touching the rest of the code. Once you have finished, you can turn it into a quiz about dinosaurs, football, space, or your favourite book, and test your friends!
A dictionary uses curly brackets and stores pairs of keys and values. Here the keys are "question", "options" and "answer". We read a value by putting its key in square brackets, like q["question"].
q = {
"question": "What does print() do?",
"options": ["a) Prints on paper", "b) Shows text on the screen", "c) Deletes your code"],
"answer": "b"
}
print(q["question"])
print("The answer is", q["answer"])
What does print() do? The answer is b
A quiz needs more than one question, so we put several dictionaries inside a list. A for loop then visits each question in turn. Inside the loop, a second for loop prints every option.
questions = [
{
"question": "What does print() do?",
"options": ["a) Prints on paper", "b) Shows text on the screen", "c) Deletes your code"],
"answer": "b"
},
{
"question": "What is 7 % 2 in Python?",
"options": ["a) 1", "b) 3.5", "c) 14"],
"answer": "a"
}
]
for q in questions:
print(q["question"])
for option in q["options"]:
print(" " + option)
What does print() do? a) Prints on paper b) Shows text on the screen c) Deletes your code What is 7 % 2 in Python? a) 1 b) 3.5 c) 14
We ask the player for a, b or c and compare their reply with the answer stored in the dictionary. .lower() means it doesn't matter if they type B or b. If they get it wrong, we tell them the right answer so they learn something.
q = {
"question": "What is 7 % 2 in Python?",
"options": ["a) 1", "b) 3.5", "c) 14"],
"answer": "a"
}
print(q["question"])
for option in q["options"]:
print(" " + option)
reply = input("Your answer (a, b or c): ").lower()
if reply == q["answer"]:
print("â
Correct!")
else:
print("â Not quite. The answer was", q["answer"])
What is 7 % 2 in Python? a) 1 b) 3.5 c) 14 Your answer (a, b or c): A â Correct!
We create score = 0 before the loop and add 1 whenever the player is right. A second variable called number counts which question we are on. We use str() to turn the number into text so it can be joined to the question with +.
score = 0
number = 1
for q in questions:
print("Question " + str(number) + ": " + q["question"])
# ...show the options and check the reply...
# when the reply is correct:
score = score + 1
number = number + 1
When the loop has asked every question, we tell the player how they did. len(questions) tells us how many questions there were, so the message is always right even if you add more questions later. Then we use if, elif and else to pick a message that matches their score.
name = "Mia"
score = 3
total = 4
print(name + ", you scored", score, "out of", total)
if score == total:
print("đ Perfect score! You're a Python star!")
elif score >= total / 2:
print("đ Great job! Keep practising.")
else:
print("đ Nice try! Read the lessons and play again.")
Mia, you scored 3 out of 4 đ Great job! Keep practising.
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
questions = [
{
"question": "What does print() do?",
"options": ["a) Prints on paper", "b) Shows text on the screen", "c) Deletes your code"],
"answer": "b"
},
{
"question": "What is 7 % 2 in Python?",
"options": ["a) 1", "b) 3.5", "c) 14"],
"answer": "a"
},
{
"question": "Which brackets make a list?",
"options": ["a) ( )", "b) { }", "c) [ ]"],
"answer": "c"
},
{
"question": "What does len('cat') give you?",
"options": ["a) 1", "b) 3", "c) 'cat'"],
"answer": "b"
}
]
score = 0
number = 1
print("â Welcome to the Python Quiz!")
name = input("What's your name? ")
for q in questions:
print()
print("Question " + str(number) + ": " + q["question"])
for option in q["options"]:
print(" " + option)
reply = input("Your answer (a, b or c): ").lower()
if reply == q["answer"]:
print("â
Correct!")
score = score + 1
else:
print("â Not quite. The answer was", q["answer"])
number = number + 1
print()
print(name + ", you scored", score, "out of", len(questions))
if score == len(questions):
print("đ Perfect score! You're a Python star!")
elif score >= len(questions) / 2:
print("đ Great job! Keep practising.")
else:
print("đ Nice try! Read the lessons and play again.")
This is what it looks like when someone plays. The words after each question are what the player typed.
â Welcome to the Python Quiz!
What's your name? Mia
Question 1: What does print() do?
a) Prints on paper
b) Shows text on the screen
c) Deletes your code
Your answer (a, b or c): b
â
Correct!
Question 2: What is 7 % 2 in Python?
a) 1
b) 3.5
c) 14
Your answer (a, b or c): a
â
Correct!
Question 3: Which brackets make a list?
a) ( )
b) { }
c) [ ]
Your answer (a, b or c): b
â Not quite. The answer was c
Question 4: What does len('cat') give you?
a) 1
b) 3
c) 'cat'
Your answer (a, b or c): B
â
Correct!
Mia, you scored 3 out of 4
đ Great job! Keep practising.Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.