How to Create a Simple Quiz Game with Python?
Jyoti Dabass, Ph.D
IIT Delhi|Sony Research|Data Science| Generative AI| LLM| Stable Diffusion|Fuzzy| Deep Learning|Cloud|AI
Are you ready to test your general knowledge and have some fun? Look no further!! In this blog, we’ll create a simple Python quiz that will challenge you with 10 questions. Don’t worry, it’s easy to understand, even if you’re new to programming. We’ll use dummy data, so you can focus on the code. For every correct answer, you’ll get 3 marks, and for every incorrect answer, you’ll lose 1 mark. At the end, we’ll calculate your total score. Let’s get started!!
Python Code
It involves six steps and the code can be tried directly in Google Colab.
??Step 1: Importing the random Module
The first thing we need to do is import the random module. This module will help us shuffle the questions, so they appear in a different order each time the game is played.
import random
??Step 2: Defining the Quiz Data
Next, we need to define the quiz data. This includes the questions, answers, and the correct answers. We’ll store this data in a list of dictionaries.
quiz_data = [
{"question": "What is the capital of France?", "answers": ["Paris", "London", "Berlin", "Rome"], "correct": 0},
{"question": "Who painted the Mona Lisa?", "answers": ["Leonardo da Vinci", "Michelangelo", "Raphael", "Caravaggio"], "correct": 0},
{"question": "What is the largest planet in our solar system?", "answers": ["Jupiter", "Saturn", "Uranus", "Neptune"], "correct": 0},
...
]
??Step 3: Shuffling the Quiz Data
Now that we have our quiz data, we need to shuffle it to randomize the order of the questions. We’ll use the random.shuffle() function to do this.
random.shuffle(quiz_data)
领英推荐
??Step 4: Initializing the Score
Before we start the game, we need to initialize the score. We’ll set it to 0 for now.
score = 0
??Step 5: Looping Through Each Question
Now it’s time to loop through each question. We’ll use a for loop to do this.
for i, question in enumerate(quiz_data):
print(f"\nQuestion {i+1}: {question['question']}")
for j, answer in enumerate(question['answers']):
print(f"{j+1}. {answer}")
# Get the user's answer
user_answer = input("Enter the number of your answer: ")
# Check if the user's answer is correct
if int(user_answer) - 1 == question['correct']:
print("Correct!")
score += 3
else:
print(f"Sorry, the correct answer is {question['answers'][question['correct']]}")
score -= 1
??Step 6: Printing the Final Score
Finally, we’ll print the final score.
print(f"\nYour final score is: {score}")
And that’s it! You’ve successfully created a simple Python quiz that tests your general knowledge with 10 questions. With this code, you can easily add or modify questions, answers, and scoring system to suit your needs. The best part? It’s easy to understand, even if you’re new to programming. You can copy and paste this code into Google Colab and start quizzing yourself or share it with friends and family.
Cheers!! Happy reading!! Keep learning!!
Please upvote, share & subscribe if you liked this!! Thanks!!
Research Scholar
2 个月Awasome