Unit 09 · lesson
Make the Player Roster Survive a Restart
Core path: 35 minutes
The Week 8 roster only exists while the Python process is running.
Close the program and the list disappears.
This lesson changes that by giving the application a second place where state can live: a file on disk.
Build the project structure
Use:
player-roster/
├── main.py
├── data/
│ └── players.json
└── README.md
Start players.json with:
[]
That file is valid JSON representing an empty list.
Load before you change anything
import json
from pathlib import Path
DATA_FILE = Path("data/players.json")
def load_players():
if not DATA_FILE.exists():
return []
with open(DATA_FILE, "r") as file:
return json.load(file)
Read the function as a decision:
file missing? → start with an empty roster
file exists? → read JSON and rebuild Python data
The program is reconstructing runtime state from persistent data.
Create and save records
def create_player():
name = input("Player name: ")
score = int(input("Score: "))
return {"name": name, "score": score, "rank": "ROOKIE"}
Save the list:
def save_players(players):
with open(DATA_FILE, "w") as file:
json.dump(players, file, indent=2)
Now coordinate the workflow:
players = load_players()
new_player = create_player()
players.append(new_player)
save_players(players)
print(f"Saved {len(players)} players.")
Prove persistence instead of assuming it
Do this exact sequence:
- run the program;
- add one player;
- stop the program completely;
- open
data/players.jsonand inspect the saved record; - run the program again;
- verify that the saved player is loaded.
The restart is important. If you never stop the process, you have not proved that the file—not leftover memory—is carrying the state forward.
Add a small menu
Build:
1. View players
2. Add player
3. Save & Quit
A while loop can keep the application alive until the user chooses to save and exit.
Make sure the program does not silently discard unsaved changes.
Break the external data
Edit players.json manually and create invalid JSON:
[
{
"name": "Nova",
"score": 850,
}
]
That trailing comma is not valid JSON.
Run the program.
The Python source file did not change. The external data did.
That is a useful shift in your debugging model: sometimes the code is valid and the input artifact is broken.
Record the parsing error, repair the JSON, and verify that loading works again.
Keep the source of truth clear
While the program is running, you have two representations:
players.json on disk
↓ load
players list in memory
↓ modify
players list in memory
↓ save
players.json on disk
They can temporarily disagree.
If you add a player in memory but have not saved yet, the file does not know about that player.
If you manually change the file while the program is already running, the in-memory list does not magically update.
That difference becomes important in almost every real application that persists state.
Success evidence
Your application should show:
Path-based data-file handling;- a load function;
- a save function;
- a list of dictionary records;
- persistence across a complete restart;
- a menu loop;
- one corrupted-JSON failure and repair; and
- a clear explanation of when memory and disk can disagree.
The goal is not merely to create players.json.
The goal is to understand which copy of the data is authoritative at each moment.