Why Track Stats?
Understand what the STATS dictionary will store and why it matters
The quiz forgets everything the moment you quit. You played five rounds, nailed the loops category, hit a personal best of 9/10 — gone.
A STATS dictionary can hold those numbers for the duration of your session. Each key is a stat you care about:
STATS = {
"games": 0,
"best_score": 0,
"best_total": 0,
"total_correct": 0,
"total_questions": 0,
}You update the values as you play. Dictionaries support direct assignment:
STATS["games"] = STATS["games"] + 1This reads the current value, adds 1, and stores the result back under the same key.
Returning multiple values
Right now run_quiz returns nothing — the score and total disappear when the function ends. To update STATS, the while loop needs those numbers. A function can return multiple values as a tuple:
def run_quiz(...):
...
return score, totalThe caller unpacks both values at once:
score, total = run_quiz(pool, num)This is the same tuple unpacking you used with QUESTIONS in the control-flow course.