Want to make a quiz game that you can play with your friends? Now you can! In this Python project, you’ll create a trivia quiz game with cool questions and answers. Get ready to test your knowledge and have some fun coding your very own quiz show! You’ll be the quiz master in no time—can you score 100%? Let’s find out!
Step 1: Setting Up Python
As with the joke generator project, make sure Python is installed on your computer. You can download it from python.org.
Step 2: Creating the Quiz Questions
Let’s start by writing down the quiz questions and their options. Each question will have four options, and one of them will be the correct answer. We’ll store this information in a dictionary, where each question is a key, and the answer options and the correct answer are the values.
# Quiz questions and answers
quiz = {
"What is the capital of France?": {
"options": ["A. Berlin", "B. Madrid", "C. Paris", "D. Rome"],
"answer": "C"
},
"Which planet is known as the Red Planet?": {
"options": ["A. Earth", "B. Mars", "C. Venus", "D. Jupiter"],
"answer": "B"
},
"Who wrote 'Harry Potter'?": {
"options": ["A. J.R.R. Tolkien", "B. J.K. Rowling", "C. George R.R. Martin", "D. Suzanne Collins"],
"answer": "B"
},
"Which is the largest mammal?": {
"options": ["A. Elephant", "B. Blue Whale", "C. Giraffe", "D. Hippopotamus"],
"answer": "B"
}
}
Step 3: Creating the Quiz Game Function
Now, let’s create a function that will display each question and ask the user to choose the correct answer. If the user gets it right, they will get a point. At the end, we’ll show the player’s score.
# Function to run the quiz
def run_quiz():
score = 0
print("Welcome to the Python Quiz Game!")
print("Answer the following questions to the best of your ability!\n")
for question, data in quiz.items():
print(question)
for option in data["options"]:
print(option)
answer = input("Your answer (A, B, C, D): ").upper()
if answer == data["answer"]:
print("Correct! Well done.\n")
score += 1
else:
print(f"Oops! The correct answer was {data['answer']}. Better luck next time!\n")
print(f"Your final score is {score}/{len(quiz)}.")
if score == len(quiz):
print("Amazing! You got all the questions right!")
else:
print("Great effort! Keep practicing to improve your score.")
Step 4: Starting the Quiz Game
Now we need to run the game. Call the run_quiz()
function to start the quiz.
# Run the quiz game
run_quiz()
Conclusion
Congratulations! You’ve just created your very own trivia quiz game in Python. This project helped you practice using dictionaries, conditional statements, and handling user input. As you improve your Python skills, you can expand this game by adding more questions, making the quiz timed, or even keeping track of high scores.
This project is a great way to show how coding can be fun and interactive. You can share it with friends and challenge them to beat your score!