Create a Quiz Game in Python: Test Your World GK

quiz game in python

Introduction

Python, with its simplicity and versatility, offers an excellent platform for developing a wide range of applications. In this article, you”ll explore how to create a quiz game in Python. Our quiz game will focus on World General Knowledge (GK), and we’ll store the quiz data in a CSV file. The game will ask the user 10 multiple-choice questions (MCQs) in each round, and the user’s score will be determined based on their answers. What’s more, after the first round, the user can continue playing for as long as they desire.

Whether you want to test someone’s knowledge on a particular topic or simply have fun, this project will give you hands-on experience in Python programming. So, let’s dive in and create our own quiz game!

Requirements

Before diving into the code, ensure you have the following requirements:

  1. Python installed on your system.
  2. Basic understanding of Python programming concepts. (To learn Python, visit this page: Python Tutorials)
  3. A code editor or IDE (Integrated Development Environment) like Visual Studio Code or PyCharm.

The Project Details

The program presents the player with general knowledge inquiries, offering four response options for each question. Each accurate response earns one point, while incorrect choices prompt the program to display the message “Incorrect answer” alongside the correct answer.

The program provides a final score based on the player’s performance in answering 10 questions. Subsequently, the player is given the option to either continue playing or not. If they choose to continue (by selecting “YES”), they will face another set of 10 questions.

CSV File

All of the questions have been sourced from the internet and are stored in a CSV file. There is a total of 25 questions available. At the end of this tutorial, you’ll find a link to the source, where you can easily find more questions to expand the CSV file.

The data follows the format described above within the CSV file. Each question is associated with four attributes, highlighted in yellow, representing the four answer choices, and one attribute, highlighted in green, representing the correct answer. You have the option to extend this dataset by including additional information. I recommend adding data in multiples of ten for optimal organization.

Setting Up Your Project

Let’s begin by setting up the project structure and creating the necessary files.

  1. Create a new directory for your project and name it something like “QuizGame”
  2. Now get the CSV file from here (questions.csv) and place it inside the project directory.
  3. Inside the project directory, create a Python script file and name it “quiz_game.py.”

Writing the Python Code

Now, let’s write the Python code for our quiz game:

import csv

score = 0
line_count = 0
applied_ques = 0

# Initialize a dictionary
options = {
    'A': 1,
    'B': 2,
    'C': 3,
    'D': 4
}

def check_guess(guess, answer):
    global score, applied_ques
    if guess == answer:
        print('Correct answer!')
        score += 1
    else:
        print(f'Wrong!, The answer is: {answer}')
    applied_ques += 1

def show_score():
    print(f'Total Score: {score}/{applied_ques}.')


if __name__ == "__main__":
    print('=====World Quiz=====')

    with open('questions.csv', 'r') as csvfile:
        csvreader = csv.reader(csvfile)

        for row in csvreader:
            if line_count == 0:
                line_count += 1
            else:
                print(f"n{row[0]}nA. {row[1]} B. {row[2]} C. {row[3]} D. {row[4]}n")
                correct_choice = False
                
                while not correct_choice:
                    guess = input("Type A, B, C, or Dn")
                    if guess.upper() in ['A', 'B', 'C', 'D']:
                        check_guess(row[options[guess.upper()]], row[5])
                        correct_choice = True
                    
                line_count += 1

                if applied_ques == 10:
                    show_score()
                    play_again = input('Want to play again?(Y/N): ')
                    if play_again in ['N', 'n']:
                        break
                    else:
                        line_count = 1
                        applied_ques = 0
                        score = 0
        if applied_ques != 10:
            show_score()

        print('Thanks for Playing! Goodbye')

In this code:

  • We import the `csv` module and declare some game parameters.
  • Next, we define an `options` dictionary to hold four keys, each representing one of the possible answers for a set of four multiple-choice questions.
  • Within the `check_guess` function, we check whether the player has selected the correct answer or not. In case of correct answer, a message will be displayed and the `score` variable will be incremented by 1. If the response is incorrect, a different message is displayed and increments the `applied_ques` variable by 1.
  • The `show_score` function displays the player’s total score at the end of the game.
  • In the main part, the program opens the CSV file in read mode and operates the entire game.

Output

To gain a thorough understanding of how to participate in this quiz game, be sure to watch the full video.

Output

Congratulations! You’ve successfully created a quiz game in Python that tests World General Knowledge and allows for continuous play. You can expand this project by adding more questions to your CSV file or customizing it further to suit your preferences. Enjoy challenging your knowledge and learning new facts!

Best of luck!

Source of the Quiz DataWorld GK Quiz Questions and Answers

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 194