Building a Simple Quiz Game in Python

Arunmathavan KArunmathavan K
2 min read

I want to share a small but fun project I built to practice my Python skills: Quiz Game. The Quiz Game is a Python-based multiple-choice game designed for learning and fun. Players are presented with a series of questions, each accompanied by four answer options. After selecting their answer, the game provides immediate feedback on whether it is correct and keeps track of the player’s score.

By playing this game, I can test my knowledge and it helped me understand concepts like loops, conditional statements, and working with lists and dictionaries. Here’s how it works:

I’ve modularized my project by placing the questions, answers, and options in a separate file named game.py.

questions=[
       {"text":"Who is known as God of Cricket?"},{"text":"Who is GOAT of Football?"},
       {"text":"What does RAM stands _________ "},{"text":"Which state is the capital of India"},
       {"text":"Who is the PM of India?"}
  ]
  answers=["A","C","B","A","D"]
  options=[["A.Sachin Tendulkar","B.Virat Kholi","C.MS Dhoni","D.Yuvraj Singh"],
           ["A.Cristiano Ronaldo","B.Neymar JR","C.Lional Messi","D.Pele"],
           ["A.Read Access Memory","B.Random Access Memory","C.Read Only Memory","D.Random Only Memory"],
           ["A.Delhi","B.Chennai","C.Mumbai","D.Kolkata"],["Amit shaw","B.Stalin","C.RN Ravi","D.Modi"]]

First import the game module. Score variable keeps track of the player’s correct answers , qo_no (Question Number) ensures the options and answers align with the current questions. Using for loop, the game cycles through each question in the list. Each question is displayed with its respective options. Input is taken and converted to uppercase for uniformity. If the player's answer matches the correct one, they score a point. Otherwise, they’re shown the correct answer. After each question, the score is updated and displayed. Once all questions are answered, the game concludes with the player’s total score.

import game
print("**************************************")
print("\nWelcome to My quiz game...")
# print("**************************************")
score=0
qo_no=0
for i in range(len(game.questions)):
    print("\n*************************************\n")
    print(game.questions[i]["text"])
    for j in game.options[qo_no]:
        print(j)
    guess=input("Enter your answers(A/B/C/D):").upper()
    if guess== game.answers[qo_no]:
        print("Correct")
        score+=1
    else:
        print("Incorrect")
        print(f"The correct answer is {game.answers[qo_no]}.")
    qo_no+=1
    print(f"Your score is:{score}/{qo_no}")
print(f"The final score is {score}")
0
Subscribe to my newsletter

Read articles from Arunmathavan K directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arunmathavan K
Arunmathavan K