How to Create a Simple Quiz Game with Python?

How to Create a Simple Quiz Game with Python?

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 Quiz

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},
   ...
]        
Python Quiz

??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        
Python Quiz

??Step 6: Printing the Final Score

Finally, we’ll print the final score.

print(f"\nYour final score is: {score}")        
Results

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.

Thanks for reading!!

Cheers!! Happy reading!! Keep learning!!

Please upvote, share & subscribe if you liked this!! Thanks!!

You can connect with me on LinkedIn, YouTube, Medium, Kaggle, and GitHub for more related content. Thanks!!

Aniruddha Mohanty

Research Scholar

2 个月

Awasome

回复

要查看或添加评论,请登录

Jyoti Dabass, Ph.D的更多文章

社区洞察

其他会员也浏览了