Kahibaro
Discord Login Register

Quiz application

Planning Your Quiz Application

A quiz app is a great mini project because it combines:

In this chapter you will build a console-based quiz application that:

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:

  1. Show a welcome message.
  2. Ask a question.
  3. Get the user’s answer.
  4. Check if the answer is correct.
  5. Update the score.
  6. Move to the next question.
  7. 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:

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:

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:

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:

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:

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 0

2. Showing Correct Answer Text

Instead of just showing the letter, show the full option text.

One way: use the same letter positions:

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 0

3. 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:

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:

You now have a complete, working console quiz that you can customize with your own questions and extend as your Python skills grow.

Views: 13

Comments

Please login to add a comment.

Don't have an account? Register now!