App 1: Your First App, a Quiz Game / Build the quiz
Lesson 9 of 10
Save high scores
Use a file to remember the best score between games. Read it at the start (if it exists) and write it when someone beats it.
Key points
- pathlib.Path makes file handling easy
- Check .exists() before reading
- Write the new best with write_text
from pathlib import Path
best_file = Path("best.txt")
best = int(best_file.read_text()) if best_file.exists() else 0
if score > best:
print("New high score!")
best_file.write_text(str(score))Your turn
Add a saved high score and show it at the start of each game.
Quiz · +10 XP
Why check best_file.exists() first?
Log in to save progress and earn XP.