Update Stats After Each Round
Exit

Update Stats After Each Round

Unpack run_quiz's return value and update the STATS dictionary

💻

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

run_quiz now returns score and total, but the while loop ignores them — it still calls run_quiz(pool, num) without capturing the result.

Unpacking the return value gives the while loop access to both numbers:

score, total = run_quiz(pool, num)

With score and total in hand, updating STATS is straightforward:

STATS["games"] = STATS["games"] + 1
STATS["total_correct"] = STATS["total_correct"] + score

The personal best check needs an if — only update when the current score exceeds the stored one:

if score > STATS["best_score"]:
    STATS["best_score"] = score
    STATS["best_total"] = total

Instructions

Unpack run_quiz's return value and update STATS after each round.

  1. Change run_quiz(pool, num) to score, total = run_quiz(pool, num) — this captures both return values so you can use them to update the stats.
  2. Add STATS["games"] = STATS["games"] + 1 — every completed round increments the game count.
  3. Add STATS["total_correct"] = STATS["total_correct"] + score — accumulates correct answers across rounds.
  4. Add STATS["total_questions"] = STATS["total_questions"] + total — accumulates total questions answered.
  5. Add an if score > STATS["best_score"]: block that sets STATS["best_score"] = score and STATS["best_total"] = total — updates the personal best only when the current round beats it.