Load Stats at Startup
Define load_stats to read the saved stats file and restore the player's history
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
save_stats writes the data. Now you need to read it back at startup so the player's history is restored.
There is one difference from load_questions: questions.json was already there when you wrote load_questions — you provided it. stats.json only exists after the first time the player quits. If you call open("stats.json", "r") before that file has been created, Python raises a FileNotFoundError and the program crashes.
You have already seen try/except in ask_question — it catches ValueError when the player types non-numeric input. The same pattern handles a missing file:
def load_stats():
try:
with open(STATS_FILE, "r") as f:
return json.load(f)
except FileNotFoundError:
return {
"games": 0,
"best_score": 0,
"best_total": 0,
"total_correct": 0,
"total_questions": 0,
}If the file exists, the try block succeeds and returns the saved stats. If it does not exist yet, FileNotFoundError is raised and the except block returns the default empty stats instead. Either way, STATS ends up as a valid dict.
Instructions
Define load_stats and use it to initialize STATS.
- Below
load_questions, define a function calledload_statswith no parameters. Inside:- Add a
try:block. Inside it, openSTATS_FILEin read mode ("r") usingwith open(...) as f:and returnjson.load(f). - Add an
except FileNotFoundError:block. Inside it, return a dict with all five keys set to0:"games","best_score","best_total","total_correct","total_questions".
- Add a
- Replace
STATS = { ... }(the entire dict literal below) withSTATS = load_stats().
import json
QUESTIONS_FILE = "questions.json"
STATS_FILE = "stats.json"
def load_questions():
with open(QUESTIONS_FILE, "r") as f:
return json.load(f)
# Step 1: define load_stats()
QUESTIONS = load_questions()
STATS = { # Step 2: replace with STATS = load_stats()
"games": 0,
"best_score": 0,
"best_total": 0,
"total_correct": 0,
"total_questions": 0,
}
def ask_question(q):
print(q["text"])
print()
print(f" 1) {q['options'][0]}")
print(f" 2) {q['options'][1]}")
print(f" 3) {q['options'][2]}")
print(f" 4) {q['options'][3]}")
print()
while True:
answer_text = input("Your answer (1-4): ")
try:
answer = int(answer_text)
except ValueError:
print("Please enter 1, 2, 3, or 4.")
continue
if answer < 1 or answer > 4:
print("Please enter 1, 2, 3, or 4.")
continue
break
is_correct = answer == q["correct"]
print()
print(f"You answered: {answer}")
if is_correct:
print("Correct!")
else:
print(f"Wrong! The correct answer was option {q['correct']}.")
return is_correct
def show_results(score, total):
print(f"Score: {score}/{total}")
if score == total:
print("Perfect score!")
elif score >= 2:
print("Great job!")
elif score >= 1:
print("Not bad!")
else:
print("Keep practicing!")
def show_stats():
if STATS["games"] == 0:
print("No games played yet.")
return
print(f"Games played: {STATS['games']}")
print(f"Best round: {STATS['best_score']}/{STATS['best_total']}")
print(f"Total correct: {STATS['total_correct']} out of {STATS['total_questions']}")
def save_stats():
with open(STATS_FILE, "w") as f:
json.dump(STATS, f, indent=2)
def run_quiz(questions=QUESTIONS, num=None):
if num is None:
num = len(questions)
score = 0
streak = 0
questions_for_round = questions[:num]
total = len(questions_for_round)
print(f"=== Python Quiz ({total} questions) ===")
print()
for q in questions_for_round:
result = ask_question(q)
if result:
score = score + 1
streak = streak + 1
if streak >= 3:
print(f" Hot streak! ({streak} in a row)")
else:
streak = 0
show_results(score, total)
return score, total
while True:
show_stats()
print()
categories = sorted(set(q["category"] for q in QUESTIONS))
print("Categories: " + ", ".join(categories))
category = input("Category (enter for all): ").strip()
if category:
pool = [q for q in QUESTIONS if q["category"] == category]
if not pool:
print("Category not found. Playing all.")
pool = QUESTIONS
else:
pool = QUESTIONS
try:
num_input = input(f"How many questions? (1-{len(pool)}, enter for all): ").strip()
if num_input:
num = int(num_input)
else:
num = len(pool)
except ValueError:
num = len(pool)
print()
score, total = run_quiz(pool, num)
STATS["games"] = STATS["games"] + 1
STATS["total_correct"] = STATS["total_correct"] + score
STATS["total_questions"] = STATS["total_questions"] + total
if score > STATS["best_score"]:
STATS["best_score"] = score
STATS["best_total"] = total
print()
again = input("Play again? (y/n): ")
if again != "y":
save_stats()
break
print("Thanks for playing!")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding