Track the Score

Add score and total variables to count how many questions the player got right

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

The quiz tells the player whether each answer is right or wrong, but it doesn't say how they did overall. With three questions in play, a running score is now meaningful.

Score variables

Add score and total before the loop:

score = 0
total = 3

score starts at zero and increments each time the player answers correctly. total is the number of questions.

Updating the score

Inside the if is_correct: block, add one to score before printing the feedback:

if is_correct:
    score = score + 1
    print("Correct!")

score = score + 1 reads the current value of score, adds one, and stores the result back.

Displaying the score

After the for loop, print the final score:

print(f"Score: {score}/{total}")

This runs once after all three questions are done and shows something like Score: 2/3.

Instructions

Add score tracking to quiz.py.

  1. Before print("=== Python Quiz ==="), create a variable named score and assign it 0.
  2. After score = 0, create a variable named total and assign it 3.
  3. Inside the if is_correct: block, before print("Correct!"), add score = score + 1.
  4. After the for loop, call print(f"Score: {score}/{total}").