Table of Contents
Planning Your Quiz Application
A quiz app is a great mini project because it combines:
- User input and output
- Conditions
- Loops
- Data collections
- Basic program structure
In this chapter you will build a console-based quiz application that:
- Asks multiple questions
- Checks if the user’s answer is correct
- Keeps track of the score
- Shows the final result
You do not need any external libraries for this.
We’ll start with a simple version, then improve it step by step.
Designing the Quiz
Before writing code, plan what your quiz should do:
- Show a welcome message.
- Ask a question.
- Get the user’s answer.
- Check if the answer is correct.
- Update the score.
- Move to the next question.
- At the end, show the total score.
In words, this is basically:
$$
\text{repeat for each question: ask → answer → check → score}
$$
You’ll turn this into code using:
print()andinput()ifstatements- A loop (
foris simplest here) - A data collection to store questions and answers
Step 1: A Single Question
Start with just one question to make sure the basic idea works.
print("Welcome to the quiz!")
score = 0
print("Question 1: What is the capital of France?")
answer = input("Your answer: ")
if answer == "Paris":
print("Correct!")
score = score + 1
else:
print("Wrong. The correct answer is Paris.")
print("Your score is:", score)This is not very flexible yet, but it proves the core logic works.
Step 2: Handling Case and Spaces
Users may type paris, PARIS, or add spaces. You can make the check more user-friendly by cleaning the input first.
Common tricks:
- Remove extra spaces with
.strip() - Make everything lowercase with
.lower()
answer = input("Your answer: ")
# Clean up the answer
answer = answer.strip().lower()
if answer == "paris":
print("Correct!")
score = score + 1
else:
print("Wrong. The correct answer is Paris.")
Now Paris, paris, PARIS are all accepted.
Step 3: Multiple Questions with a List
Instead of copying and pasting code for each question, store questions and answers in a list of dictionaries (or tuples). This makes your quiz easier to change.
Here is one way using dictionaries:
questions = [
{
"question": "What is the capital of France?",
"answer": "paris"
},
{
"question": "What is 5 + 7?",
"answer": "12"
},
{
"question": "What is the color of the sky on a clear day?",
"answer": "blue"
}
]Then loop through this list:
print("Welcome to the quiz!")
score = 0
for q in questions:
print()
print(q["question"])
user_answer = input("Your answer: ")
user_answer = user_answer.strip().lower()
if user_answer == q["answer"]:
print("Correct!")
score = score + 1
else:
print("Wrong. The correct answer is", q["answer"] + ".")
print()
print("You got", score, "out of", len(questions), "correct.")You now have a complete basic quiz.
Step 4: Multiple-Choice Questions
Multiple-choice questions are easier for users and allow you to guide answers.
You can store:
- The question text
- A list of options
- The correct option (for example,
"a","b", etc.)
questions = [
{
"question": "What is the capital of France?",
"options": ["a) Berlin", "b) Madrid", "c) Paris", "d) Rome"],
"answer": "c"
},
{
"question": "Which number is even?",
"options": ["a) 3", "b) 7", "c) 10", "d) 9"],
"answer": "c"
}
]Loop with options:
print("Welcome to the multiple-choice quiz!")
score = 0
for q in questions:
print()
print(q["question"])
for option in q["options"]:
print(option)
user_answer = input("Your answer (a, b, c, or d): ")
user_answer = user_answer.strip().lower()
if user_answer == q["answer"]:
print("Correct!")
score = score + 1
else:
print("Wrong. The correct answer was", q["answer"])
print()
print("You scored", score, "out of", len(questions))Step 5: Adding Feedback and Percentages
A quiz feels nicer when it gives more than just the raw number.
You can calculate a percentage:
$$
\text{percentage} = \frac{\text{correct answers}}{\text{total questions}} \times 100
$$
And then give feedback based on that.
total_questions = len(questions)
percentage = (score / total_questions) * 100
print()
print("You scored", score, "out of", total_questions)
print("Percentage:", round(percentage, 2), "%")
if percentage == 100:
print("Amazing! Perfect score!")
elif percentage >= 70:
print("Well done!")
elif percentage >= 50:
print("Not bad, but you can improve.")
else:
print("Keep practicing!")Step 6: Organizing Code with Functions
To make the quiz easier to read and extend, you can separate parts into functions:
- A function to run the whole quiz
- A function to ask a single question and return
1or0for correct/incorrect
def ask_question(q):
print()
print(q["question"])
for option in q["options"]:
print(option)
user_answer = input("Your answer (a, b, c, or d): ")
user_answer = user_answer.strip().lower()
if user_answer == q["answer"]:
print("Correct!")
return 1
else:
print("Wrong. The correct answer was", q["answer"])
return 0
def run_quiz(questions):
print("Welcome to the quiz!")
score = 0
for q in questions:
score = score + ask_question(q)
total_questions = len(questions)
percentage = (score / total_questions) * 100
print()
print("You scored", score, "out of", total_questions)
print("Percentage:", round(percentage, 2), "%")
# Define questions
questions = [
{
"question": "What is the capital of France?",
"options": ["a) Berlin", "b) Madrid", "c) Paris", "d) Rome"],
"answer": "c"
},
{
"question": "Which number is even?",
"options": ["a) 3", "b) 7", "c) 10", "d) 9"],
"answer": "c"
}
]
# Start the quiz
run_quiz(questions)This structure makes it easier to:
- Add more questions
- Change how questions are asked
- Reuse
run_quizwith different question sets
Step 7: Simple Enhancements
Once your basic quiz works, you can add small features. Here are a few that stay within beginner-friendly Python.
1. Limiting Invalid Answers
For multiple choice, you may want to force the user to enter only a, b, c, or d.
def get_valid_answer():
while True:
user_answer = input("Your answer (a, b, c, or d): ")
user_answer = user_answer.strip().lower()
if user_answer in ["a", "b", "c", "d"]:
return user_answer
else:
print("Please enter a, b, c, or d.")
def ask_question(q):
print()
print(q["question"])
for option in q["options"]:
print(option)
user_answer = get_valid_answer()
if user_answer == q["answer"]:
print("Correct!")
return 1
else:
print("Wrong. The correct answer was", q["answer"])
return 02. Showing Correct Answer Text
Instead of just showing the letter, show the full option text.
One way: use the same letter positions:
a→ index0b→ index1c→ index2d→ index3
def ask_question(q):
print()
print(q["question"])
for option in q["options"]:
print(option)
letters = ["a", "b", "c", "d"]
correct_index = letters.index(q["answer"])
correct_text = q["options"][correct_index]
user_answer = get_valid_answer()
if user_answer == q["answer"]:
print("Correct!")
return 1
else:
print("Wrong. The correct answer was:", correct_text)
return 03. Tracking Which Questions Were Wrong
You can keep a list of incorrectly answered questions and show them at the end.
def run_quiz(questions):
print("Welcome to the quiz!")
score = 0
wrong_questions = []
for q in questions:
result = ask_question(q)
if result == 1:
score = score + 1
else:
wrong_questions.append(q["question"])
total_questions = len(questions)
percentage = (score / total_questions) * 100
print()
print("You scored", score, "out of", total_questions)
print("Percentage:", round(percentage, 2), "%")
if wrong_questions:
print("\nYou missed these questions:")
for wq in wrong_questions:
print("-", wq)Step 8: Possible Extensions (Ideas to Explore)
When you are comfortable with the basic version, you can extend your quiz using topics from other chapters:
- Random order of questions (using
random.shuffle()from the libraries chapter) - Load questions from a file (from the files chapter)
- Timed quiz (using simple time functions)
- Category-based quizzes (separate lists for different topics)
- Difficulty levels (easy / medium / hard question sets)
Keep those ideas in mind as you continue the course; you can come back and improve your quiz as you learn more.
Summary
In this quiz application mini project you:
- Designed a simple quiz flow
- Created data structures to store questions and answers
- Used loops and conditions to process user answers
- Calculated a score and percentage
- Organized the program with functions
- Added small user-friendly features
You now have a complete, working console quiz that you can customize with your own questions and extend as your Python skills grow.