1 The Problem
We want a quiz: the program asks several questions, checks each answer, keeps score, and shows the final result. It teaches checking input against a correct answer and keeping a score across many steps — the heart of any graded or scored program.
2 How to Think About It
The plan, before any code:
3 The Build — explained part by part
Here is the complete quiz. Each part is explained below.
# A short quiz that keeps score.
# Each question paired with its correct answer.
questions = [
("What is 2 + 2? ", "4"),
("What colour is the sky on a clear day? ", "blue"),
("What is the capital of France? ", "Paris"),
]
score = 0 # start the score at zero
for question, correct_answer in questions:
answer = input(question)
# Compare ignoring capitals and spaces, so "paris" counts.
if answer.strip().lower() == correct_answer.lower():
print("Correct!")
score = score + 1
else:
print(f"Wrong. The answer was {correct_answer}.")
print(f"You scored {score} out of {len(questions)}.")
score = 0 — start the score at zero before any questions.
for question, correct_answer in questions: — go through the list one pair at a time, unpacking each into the question text and its answer.
answer.strip().lower() — clean up the player’s answer:
strip() removes accidental spaces, lower() makes it lowercase, so “Paris”, “paris”, and “ paris ” all count as correct.score = score + 1 — add a point for a correct answer. This is the score carrying across questions.
len(questions) — the total number of questions, so we can show “3 out of 3” without hard-coding the number.
answer == correct_answer without cleaning — then “Paris ” with a space, or “paris” lowercase, counts as wrong..strip().lower() so fair answers count.score = 0 inside the loop — it resets to zero every question.len(questions) so the total updates automatically.4 Test & Prove Each Part
We test the scoring logic — given a set of answers, does it count the right number correct? — without needing a person to type.
QUESTIONS = [
("q1", "4"),
("q2", "blue"),
("q3", "Paris"),
]
def score_quiz(answers):
"""Given a list of answers, return how many are correct."""
score = 0
for (question, correct), given in zip(QUESTIONS, answers):
if given.strip().lower() == correct.lower():
score += 1
return score
def test_all_correct():
"""All right answers give a full score."""
assert score_quiz(["4", "blue", "Paris"]) == 3
def test_all_wrong():
"""All wrong answers give zero."""
assert score_quiz(["1", "red", "London"]) == 0
def test_case_and_spaces_ignored():
"""Capitals and spaces do not matter."""
assert score_quiz(["4", " BLUE ", "paris"]) == 3
Run with pytest -v. The score_quiz function holds the logic, so tests can feed it answers directly. Testing that “ BLUE ” still counts proves our cleaning works.
5 The Interface
What it expects
What is 2 + 2? 4
What colour is the sky? blue
Capital of France? ParisWhat it returns
Correct!
Correct!
Correct!
You scored 3 out of 3.6 Run It & Automate It
python3 quiz.pyAnswer each question and see your final score.
Jenkins runs the tests automatically — each line explained below.
What is 2 + 2? 4
Correct!
What colour is the sky on a clear day? blue
Correct!
What is the capital of France? Paris
Correct!
You scored 3 out of 3.answer.strip().lower() and lowercase the correct answer too.score = 0 is before the loop, and score + 1 is inside the correct branch.// Jenkinsfile — automated tests on every change.
pipeline {
agent any
stages {
stage('Get the code') { steps { checkout scm } } // download the code
stage('Install') { steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } } // test tool
stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } } // run every test
}
post {
success { echo 'All tests passed.' }
failure { echo 'A test failed — look above.' }
}
}
- More questions. Just add pairs to the list — the score and total adjust automatically. (Teaches: data-driven design.)
- Track wrong ones. Show which questions were missed at the end. (Teaches: collecting into a list.)
- A grade. Turn the score into a percentage and a letter grade. (Teaches: maths + branches.)
strip() and lower() so answers count fairly. Related: Loops, String Methods.