Building a Quiz App in Python
A teen-friendly Python project: build a working quiz app from scratch using variables, lists, dictionaries, loops, conditionals and functions, keep score, and give feedback, with full runnable code and a quiz.
Key takeaways
- Store each question, its options and answer together in a dictionary
- Keep all questions in a list so you can loop over them
- Use input() to get the player's answer and compare it to the correct one
- Keep a running score variable, adding 1 for each correct answer
- Wrap logic in functions to keep the program organized and reusable
What we're building
In this project you'll build a complete quiz app in Python that runs in the terminal. It asks a series of multiple-choice questions, reads the player's answers, checks if they are right, keeps score, and shows a final result with a friendly message.
Along the way you'll combine almost everything from the basics: variables, lists, dictionaries, loops, conditionals and functions. By the end you'll have a real, working program you can show off and extend.
Step 1: Store one question
The first decision is how to store a question. A question has three parts that belong together: the question text, the options, and the correct answer. A dictionary is perfect for grouping related fields under names.
question = {
"question": "What is the capital of France?",
"options": ["London", "Paris", "Rome"],
"answer": 2
}
Here "answer": 2 means option number 2 ("Paris") is correct. We'll number the options for the player starting at 1, so "Paris" is choice 2. Storing data this way keeps everything for one question in a single, tidy object.
Step 2: Make a list of questions
A quiz needs many questions. We store them in a list of dictionaries so we can loop over them:
questions = [
{
"question": "What is the capital of France?",
"options": ["London", "Paris", "Rome"],
"answer": 2
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["Mars", "Jupiter", "Venus"],
"answer": 1
},
{
"question": "What is 7 multiplied by 6?",
"options": ["42", "36", "48"],
"answer": 1
}
]
Now questions is one variable holding all three questions in order. Adding a fourth question later is as easy as adding another dictionary.
Step 3: Show one question
Let's write a function that displays a single question and its numbered options. (For the full idea of functions, see Functions Explained.)
def show_question(q):
print(q["question"])
for i in range(len(q["options"])):
print(i + 1, "-", q["options"][i])
Reading it carefully:
q["question"]pulls the question text out of the dictionary.range(len(q["options"]))gives the index numbers 0, 1, 2 for the options.- We print
i + 1so the player sees choices numbered 1, 2, 3 instead of 0, 1, 2.
For the France question it prints:
What is the capital of France?
1 - London
2 - Paris
3 - Rome
Step 4: Get and check the answer
Now we read the player's choice and compare it to the correct answer. Remember that input() always returns a string, so we convert it to a number with int().
def ask_question(q):
show_question(q)
choice = int(input("Your answer (number): "))
if choice == q["answer"]:
print("Correct! β
")
return True
else:
correct_option = q["options"][q["answer"] - 1]
print("Wrong. The answer was:", correct_option)
return False
Step by step:
show_question(q)prints the question and options.int(input(...))reads the player's number choice.- The
ifcompareschoicewithq["answer"](see Making Decisions with If). - If they match, we congratulate the player and
return True. - Otherwise we look up the correct option text (
q["answer"] - 1because the list starts at index 0) andreturn False.
Returning True or False lets the main loop know whether to add a point.
Step 5: Loop through all questions and keep score
This is where it all comes together. We start a score variable at 0, loop through every question, and add 1 each time the player is right.
def run_quiz(questions):
score = 0
for q in questions:
print() # blank line for spacing
if ask_question(q):
score += 1
return score
The for q in questions loop walks through the list one dictionary at a time. ask_question(q) returns True or False, and because of that, if ask_question(q): adds a point only on a correct answer. The score += 1 is shorthand for score = score + 1.
Step 6: Put it all together
Here is the complete, runnable program. Copy it into a file called quiz.py and run it.
questions = [
{
"question": "What is the capital of France?",
"options": ["London", "Paris", "Rome"],
"answer": 2
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["Mars", "Jupiter", "Venus"],
"answer": 1
},
{
"question": "What is 7 multiplied by 6?",
"options": ["42", "36", "48"],
"answer": 1
}
]
def show_question(q):
print(q["question"])
for i in range(len(q["options"])):
print(i + 1, "-", q["options"][i])
def ask_question(q):
show_question(q)
choice = int(input("Your answer (number): "))
if choice == q["answer"]:
print("Correct! β
")
return True
else:
correct_option = q["options"][q["answer"] - 1]
print("Wrong. The answer was:", correct_option)
return False
def run_quiz(questions):
score = 0
for q in questions:
print()
if ask_question(q):
score += 1
return score
print("π Welcome to the Python Quiz! π")
final_score = run_quiz(questions)
total = len(questions)
print()
print("You scored", final_score, "out of", total)
if final_score == total:
print("Perfect score! Amazing! π")
elif final_score >= total / 2:
print("Nice work! π")
else:
print("Keep practicing! πͺ")
Run it and play through. The program greets the player, asks each question, checks answers, and prints a final score with a message that depends on how well they did. That final if / elif / else is a small example of giving feedback based on a value.
How the program flows
It helps to see the whole shape:
- The
questionslist holds the data. run_quizloops over that data.- For each question,
ask_questionshows it (viashow_question), reads input, and checks the answer. - Correct answers bump the
score. - After the loop, the main code reports the total and prints a tailored message.
Notice how each function does one job. That separation makes the program easy to read and easy to change.
Try it yourself
- More questions: Add three of your own questions to the list. The loop handles them automatically β no other code changes needed.
- Guard against bad input: Right now typing a letter crashes the program. Wrap the input in a check, or use a loop that keeps asking until the player types a valid number 1β3.
- Shuffle the questions: Add
import randomat the top and callrandom.shuffle(questions)beforerun_quizso the order changes every game. - Track wrong answers: Build a list of the questions the player missed and print them at the end so they can review.
Challenge: add a category field to each question dictionary, then let the player choose a category at the start and only quiz them on questions in that category. This combines lists, dictionaries, conditionals and loops β the full toolkit of a real app. Once you've done that, you've built something genuinely useful from scratch. Well done!
Quick quiz
Test yourself and earn XP
Which data structure best stores one question with its options and answer together?
A dictionary holds named fields like 'question', 'options' and 'answer' in one tidy object, perfect for grouping related data.
Why put all the questions in a list?
A list holds many items in order, so a for-loop can walk through each question automatically.
What does input() return when the player types '2'?
input() always returns a string. To compare it as a number you convert it with int(), or compare strings directly.
How do you keep score across all questions?
Initialize score = 0 before the loop, then score += 1 whenever the answer is correct. After the loop, score holds the total.
What is the benefit of putting the quiz logic in a function?
Functions group related steps under a name so the program is organized, reusable, and easier to read and test.
FAQ
No. The whole project uses only built-in Python features: variables, lists, dictionaries, loops, if-statements and functions. A standard Python install is all you need.
Just add another dictionary to the questions list with its 'question', 'options' and 'answer' fields. The loop automatically handles however many questions are in the list.
Keep exploring
More in Coding