Editor

Python Program for a Quiz Game

Beginner · Games and random numbers

A quiz game asks questions, checks each answer and keeps score. This program stores the questions and their answers as a list of pairs, marks a set of sample answers, and prints the final score with a message.

Program

quiz_game.py
questions = [
("What is the capital of India?", "new delhi"),
("How many days are there in a leap year?", "366"),
("Which planet is known as the Red Planet?", "mars"),
("What is 7 x 8?", "56"),
("Which keyword starts a function in Python?", "def"),
]
# The player's answers. To play for real, ask in the loop instead:
# answer = input(question + " ")
answers = ["New Delhi", "365", "Mars", "56", " def "]
score = 0
for i in range(len(questions)):
question, correct = questions[i]
answer = answers[i].strip().lower() # ignore extra spaces and capitals
print(f"Q{i + 1}. {question}")
print(" Your answer:", answers[i].strip())
if answer == correct:
print(" Correct!")
score += 1
else:
print(" Wrong, the answer is", correct)
print(f"You scored {score} out of {len(questions)}")
if score == len(questions):
print("Perfect score!")
elif score >= 3:
print("Well done!")
else:
print("Keep practising!")
Output
Q1. What is the capital of India?
    Your answer: New Delhi
    Correct!
Q2. How many days are there in a leap year?
    Your answer: 365
    Wrong, the answer is 366
Q3. Which planet is known as the Red Planet?
    Your answer: Mars
    Correct!
Q4. What is 7 x 8?
    Your answer: 56
    Correct!
Q5. Which keyword starts a function in Python?
    Your answer: def
    Correct!
You scored 4 out of 5
Well done!

How it works

  1. Each question is stored with its answer as a pair, so question, correct = questions[i] unpacks them in one line.
  2. strip() removes extra spaces and lower() ignores capital letters, so "New Delhi" and " def " still count as correct.
  3. A right answer adds 1 to score, and a wrong one shows the correct answer.
  4. The final message depends on the score, which is 4 out of 5 here, because a leap year has 366 days.

Another way to write it

A multiple-choice quiz

A multiple-choice quiz
quiz = [
("Which of these is a list?", ["(1, 2)", "[1, 2]", "{1: 2}"], "b"),
("What does len('hello') return?", ["4", "5", "6"], "b"),
("Which symbol starts a comment in Python?", ["#", "//", "--"], "a"),
]
picks = ["b", "c", "a"] # or: pick = input("Your choice (a, b or c): ")
score = 0
for (question, options, correct), pick in zip(quiz, picks):
print(question)
for letter, option in zip("abc", options):
print(f" {letter}) {option}")
if pick == correct:
print(" You chose", pick, "- correct!")
score += 1
else:
print(" You chose", pick, "- the answer is", correct)
print(f"Score: {score}/{len(quiz)}")
Output
Which of these is a list?
  a) (1, 2)
  b) [1, 2]
  c) {1: 2}
  You chose b - correct!
What does len('hello') return?
  a) 4
  b) 5
  c) 6
  You chose c - the answer is b
Which symbol starts a comment in Python?
  a) #
  b) //
  c) --
  You chose a - correct!
Score: 2/3

zip() walks through two lists side by side, pairing each question with the player's pick, and zip("abc", options) labels the options a, b and c.

Try it yourself

Add two questions of your own and print the score as a percentage too.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises