0%

Final Capstone: Build a Quiz Engine · practice

Loop Through the Quiz and Track the Score

One complete question works. The next layer processes the .

run_questions(plan) should ask every question and return the final score.

Start with the score

The accumulator begins at zero:

score = 0

For each question:

  1. show its position;

  2. call the one-question ;

  3. add 1 only when the returned is True.

Return the score after the .

Show human-friendly positions

The list gives you each question in turn, but nothing in a for loop tells you which number you are on. The player should see positions counting from 1.

You already have the tool for that. It is the counter from Chapter 7, and it is the same three parts as any accumulator:

total = len(questions)
number = 1

for question in questions:
    print(f"Question {number} of {total}")
    number += 1

number starts before the loop, moves once per pass, and is used inside the body. Measuring total before the loop gives every question the same total. If the list is empty, the loop displays no questions and the score stays zero.

Keep scoring outside ask_question

ask_question answers one question and returns a Boolean. It should not reach into a global score.

The loop owns the accumulator because it knows when a complete quiz begins and ends.

This design makes the one-question function reusable. A future practice mode could call it without maintaining a score.

Return before reporting

This milestone returns the integer score but does not print the final report yet.

That separation lets you check:

score = run_questions(plan)
assert score == 2

The following milestone will decide how to present the number.

Test a mixed path

A useful three-question run includes:

  • one correct answer;

  • one incorrect answer;

  • one correct answer with different capitalization or surrounding spaces.

The expected score is 2. This checks accumulation, both feedback paths, and the shared cleaning rule.

The answers produce True, False, and True. What score should the loop return?

Task

Implement run_questions(plan).

  • Start score at 0.

  • through every record in plan["questions"].

  • Before each question, print a blank line and Question POSITION of TOTAL.

  • Call ask_question for that record.

  • Add 1 only when it returns True.

  • Return the final score after the loop.

Keep all earlier . Submit checks different question counts and combinations of correct and incorrect responses.

To try this function with Run, temporarily add this call below the definitions and existing assertions:

print(run_questions(build_quiz_plan()))

Use your own questions for an all-correct run and a mixed run. Check the numbering and compare the returned score with your hand count.

Remove the temporary call before Submit and before continuing. Keep the noninteractive assertions; they check the helpers without asking for input.