Project Overview 💡
In this project, you'll build a basic multiple-choice quiz game in the terminal. You'll learn how to use functions, lists, conditionals, and user input to create an interactive experience.
Challenge Yourself! 🚀
Before checking the solution, try building a program that displays quiz questions with multiple options and lets the user answer each one.
Task:
Write a Python program that:
Stores a list of multiple-choice questions
Asks each question and displays the options
Takes the user’s input and checks if it’s correct
Calculates and displays the final score
Expected Output:
1. The program starts by providing the question and choices and asking the user to pick an answer (1, 2, 3, or 4):
The user enters a number (e.g., 2). The program prints out “Correct! if the answer is correct:
The program shows the second question (e.g., Which planet is known as the Red Planet?”). If the user enters a wrong answer, the program prints the relevant message. The process is repeated with a third question:
Finally, the program prints out the total score to the user after three questions:
To make your job easier, here are the data from the above quiz:
quiz_questions = [ ("What is the capital of France?", ["Berlin", "Paris", "Madrid", "Rome"], 2), ("Which planet is known as the Red Planet?", ["Earth", "Venus", "Mars", "Jupiter"], 3), ("What is the chemical symbol for water?", ["H2O", "O2", "CO2", "H2"], 1), ]
Give it a shot! Scroll down when you're ready for the step-by-step guide.
Spoiler Alert!
Step-by-Step Guide
Step 1️⃣: Define a Function to Ask a Question
This function prints a question, displays options, and checks the user's answer.
def ask_question(question, options, correct_answer):
print(question)
for i, option in enumerate(options, 1):
print(f"{i}. {option}")
answer = input("Your answer (1/2/3/4): ").strip()
if answer.isdigit() and int(answer) == correct_answer:
print("Correct!\n")
return True
else:
print(f"Wrong! The correct answer was {correct_answer}. {options[correct_answer - 1]}\n")
return False
Step 2️⃣: Define a Function to Run the Quiz
This function loops through each question and tracks the score.
def run_quiz(questions):
score = 0
for question, options, correct_answer in questions:
if ask_question(question, options, correct_answer):
score += 1
print(f"Quiz completed! Your final score is {score}/{len(questions)}.\n")
Step 3️⃣: Define the List of Questions
Each question is stored as a tuple: (question, options list, correct option index).
quiz_questions = [
("What is the capital of France?", ["Berlin", "Paris", "Madrid", "Rome"], 2),
("Which planet is known as the Red Planet?", ["Earth", "Venus", "Mars", "Jupiter"], 3),
("What is the chemical symbol for water?", ["H2O", "O2", "CO2", "H2"], 1),
]
Step 4️⃣: Start the Quiz
Call the run_quiz()
function and pass in the list of questions.
run_quiz(quiz_questions)
Complete Code 🧨
# Simple Quiz Game
def ask_question(question, options, correct_answer):
print(question)
for i, option in enumerate(options, 1):
print(f"{i}. {option}")
# Get user's answer
answer = input("Your answer (1/2/3/4): ").strip()
# Check if the answer is correct
if answer.isdigit() and int(answer) == correct_answer:
print("Correct!\n")
return True
else:
print(f"Wrong! The correct answer was {correct_answer}. {options[correct_answer - 1]}\n")
return False
def run_quiz(questions):
score = 0
# Iterate through the list of questions
for question, options, correct_answer in questions:
if ask_question(question, options, correct_answer):
score += 1
# Display the final score
print(f"Quiz completed! Your final score is {score}/{len(questions)}.\n")
# List of questions (question, options, correct_answer)
quiz_questions = [
("What is the capital of France?", ["Berlin", "Paris", "Madrid", "Rome"], 2),
("Which planet is known as the Red Planet?", ["Earth", "Venus", "Mars", "Jupiter"], 3),
("What is the chemical symbol for water?", ["H2O", "O2", "CO2", "H2"], 1),
]
# Run the quiz
run_quiz(quiz_questions)
Alternative Solution
This version uses a dictionary for questions and shuffles the order:
import random
def ask_question(question, choices, answer):
print(question)
for i, choice in enumerate(choices, 1):
print(f"{i}. {choice}")
user_input = input("Your answer: ")
return user_input.strip() == str(answer)
questions = [
{"q": "Capital of France?", "choices": ["Paris", "Rome", "Madrid", "Berlin"], "answer": 1},
{"q": "Red Planet?", "choices": ["Mars", "Venus", "Jupiter", "Earth"], "answer": 1},
]
random.shuffle(questions)
score = 0
for q in questions:
if ask_question(q["q"], q["choices"], q["answer"]):
print("Correct!\n")
score += 1
else:
print("Wrong!\n")
print(f"Your score: {score}/{len(questions)}")
Comparison
The original version uses tuples and a clean, function-based design.
The alternative version introduces shuffling and dictionary structures for more flexibility.
Enjoyed this project? Unlock real-world projects with full guides & solutions by subscribing to the paid plan. 🎉
#Build a Simple Quiz Game
def new_game():
correct_guesses = 0
numeric_options = 1
guesses = []
print("Welcome to a Harry Potter Quiz Game")
print(" ")
for key in questions:
print("------------------------------")
print(key)
for j in options[numeric_options - 1]:
print(j)
numeric_options += 1
awnser = input("Enter A, B, C, or D: ")
awnser = awnser.upper()
guesses.append(awnser)
correct_guesses += check_answer(questions.get(key), awnser)
show_score(correct_guesses,guesses)
print("-----------------------------")
print()
play_game()
def check_answer(awnsers,awnser):
if awnser == awnsers:
print("YOU GOT IT!")
return 1
else:
print("Wrong!")
return 0
def show_score(correct_guesses,guesses):
print()
print("RESULTS!")
print("AWNSERS: ", end=" ")
for i in questions:
print(questions.get(i), end=" ")
print()
print("GUESSES: ", end=" ")
for i in guesses:
print(i, end=" ")
print()
score = int((correct_guesses/len(questions))*100)
print(f"Your score is : {score}%")
def play_game():
while True:
response = input("Wanna play againg?: ")
if response == "yes" or response == "y":
new_game()
else:
break
print("Thanks for playing!")
questions = {
"Who is the actor who played Harry Potter?:": "A",
"Where is the movie 'Harry Potter' set?: ": "A",
"Who writes the 'Harry Potter' books?: ": "D",
"What is the name of Harry Potter's best friend?: ": "C",
"How many books are in the 'Harry Potter' series?: ": "B",
"How many movies are in the 'Harry Potter' series?: ": "D"
}
options = [["A.Daniel Radcliffe", "B.Emma Watson", "C.Rupert Grint", "D.Tom Felton"],
["A.Hogwarts", "B.London", "C.New York", "D.Paris"],
["A.J.R.R. Tolkien", "B.Stephen King", "C.George R.R. Martin", "D.J.K. Rowling"],
["A.Neville Longbottom", "B.Hermione Granger", "C.Ron Weasley", "D.Draco Malfoy"],
["A.6", "B.7", "C.8", "D.9"],
["A.5", "B.7", "C.6", "D.8"]]
new_game()