thecodex.expert · The Codex Family of Knowledge
Tier 0 · Absolute Beginner · Python Project

Basic Quiz

Ask a series of questions, check the answers, and show a score. Your first program that keeps track of how you are doing.

🧠 Teaches how to think spoonfed, every age Last verified:

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.

Where this shows up: quizzes and tests, form validation, games with points, surveys, any feature that compares what the user did against what was expected and tallies a result.

2 How to Think About It

The plan, before any code:

The plan — in plain English
1. Start the score at zero. → 2. For each question: ask it, read the answer, and if it matches the correct one, add a point. → 3. At the end, show the score out of the total. The key idea is a score that carries across all the questions.

Yes

No

Score = 0

Ask question 1

Correct?

Score + 1

Next question

...more questions...

Show final score

3 The Build — explained part by part

Here is the complete quiz. Each part is explained below.

Pythonquiz.py
# 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)}.")
What each part does — in plain words
questions = [ ... ] — a list of questions, each paired with its correct answer. Pairing them keeps each question next to its answer.

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.
Common mistakes — and how to avoid them
✗ Comparing answer == correct_answer without cleaning — then “Paris ” with a space, or “paris” lowercase, counts as wrong.
✓ Clean both sides with .strip().lower() so fair answers count.
✗ Putting score = 0 inside the loop — it resets to zero every question.
✓ Set the score to 0 once, before the loop.
✗ Hard-coding “out of 3” — then adding a question makes the total wrong.
✓ Use 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.

All correct answers score full marks
All wrong answers score zero
Answers count regardless of capitals or spaces
Pythontest_quiz.py
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

INPUTanswersone per question
What it expects
What is 2 + 2? 4
What colour is the sky? blue
Capital of France? Paris
OUTPUTscoremarks per answer + total
What it returns
Correct!
Correct!
Correct!
You scored 3 out of 3.

6 Run It & Automate It

Run it locally
python3 quiz.py
Answer each question and see your final score.

Jenkins runs the tests automatically — each line explained below.

What you should see when it works
Terminala real run
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.
If it breaks — how to fix it
🚨 Correct answers are marked wrong.
You are probably not cleaning the input. Use answer.strip().lower() and lowercase the correct answer too.
🚨 The score is always 0 or always full.
Check that score = 0 is before the loop, and score + 1 is inside the correct branch.
GroovyJenkinsfile
// 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.' }
    }
}
🎯 Try this next — make it yours
  1. More questions. Just add pairs to the list — the score and total adjust automatically. (Teaches: data-driven design.)
  2. Track wrong ones. Show which questions were missed at the end. (Teaches: collecting into a list.)
  3. A grade. Turn the score into a percentage and a letter grade. (Teaches: maths + branches.)
What you learned
You learned to store paired data (questions with answers), keep a score across many steps, and clean user input with strip() and lower() so answers count fairly. Related: Loops, String Methods.