Python Quiz Game
Learn how to build a quiz game using Python lists, dictionaries, loops, conditions, user input and score calculation.
1. Project Introduction
A quiz game displays questions to the player and asks them to select or enter an answer.
The program checks the answer, updates the score and finally displays the player's total score.
2. Creating Quiz Questions
We can store questions in a Python list.
questions = [
{
"question": "What is Python?",
"options": [
"A programming language",
"A database",
"An operating system",
"A browser"
],
"answer": 1
}
]
Each question can be stored as a dictionary containing the question, options and correct answer.
3. Understanding the Question Structure
The dictionary gives us separate values for different parts of the question.
question["question"] question["options"] question["answer"]
This makes it easy to access the required information while the quiz is running.
4. Creating Multiple Questions
We can add more dictionaries to the list.
questions = [
{
"question": "What is Python?",
"options": [
"Programming Language",
"Database",
"Browser",
"OS"
],
"answer": 1
},
{
"question": "Which keyword creates a function?",
"options": [
"function",
"def",
"func",
"create"
],
"answer": 2
}
]
5. Using a Score Variable
We need a variable to store the player's score.
score = 0
Whenever the player answers correctly, we increase the score.
score += 1
6. Looping Through Questions
A for loop allows us to process every question.
for question in questions:
print(question["question"])
The loop runs once for every question in the list.
7. Displaying Options
Each question contains a list of options. We can use another loop to display them.
for index, option in enumerate(question["options"]):
print(index + 1, option)
8. Taking the Player's Answer
After displaying the options, we ask the player to enter an answer.
answer = int(
input("Enter your answer: ")
)
9. Checking the Answer
Now compare the player's answer with the correct answer stored in the question.
if answer == question["answer"]:
print("Correct!")
score += 1
else:
print("Wrong answer.")
10. Calculating the Final Score
After all questions have been completed, we can display the final score.
print("Quiz Completed!")
print(
"Your score:",
score,
"/",
len(questions)
)
11. Calculating Percentage
We can calculate the percentage using the score and total number of questions.
percentage = (
score / len(questions)
) * 100
print("Percentage:", percentage)
12. Adding a Result Message
We can display a different message based on the percentage.
if percentage >= 80:
print("Excellent performance!")
elif percentage >= 50:
print("Good job!")
else:
print("Keep practicing!")
13. Handling Invalid Answers
The user may enter an option outside the available range. We can validate the input.
if answer < 1 or answer > len(question["options"]):
print("Invalid option")
14. Complete Quiz Flow
15. Concepts You Practiced
- Python Lists
- Python Dictionaries
- Nested Data
- for Loops
- enumerate()
- if / elif / else
- input()
- int()
- Variables
- Score Calculation
- Percentage Calculation
- Input Validation
16. Run Complete Project 🚀
You have learned how the Python Quiz Game works. Now open the complete working project.
Run Complete Project