🎯 MILESTONE 3 - Multi-user flashcard app

If you’ve managed to complete exercises 17 to 24, you have built a robust file-based multi-user flashcard app while learning about file input/output, JSON, and error handling.

🔑 Core Concepts Mastered

File Input/Output

  • Opening Files: Using open() with different modes ("r", "w", "a")
    • "r" = read (load data)
    • "w" = write (save data, overwrites file)
    • "a" = append (add to end of file)
  • File Context Managers: Using with statements
    • Automatically closes files
    • Handles errors safely
    • Pattern: with open(filename, mode) as file:
  • Reading Files:
    • file.read() - Read entire file as string
    • file.readlines() - Read as list of lines
    • for line in file: - Process line by line

JSON Format

  • What is JSON?: JavaScript Object Notation - universal data format
    • Looks like Python dictionaries
    • Human-readable and machine-readable
    • Industry standard for data exchange
  • JSON Operations:
    • json.load(file) - Read JSON from file → Python object
    • json.dump(data, file) - Write Python object → JSON file
    • indent=2 parameter - Makes JSON human-readable
  • JSON Data Types:
    • Dictionaries ({}) → JSON objects
    • Lists ([]) → JSON arrays
    • Strings, numbers, booleans, null

Error Handling for Files

  • Try/Except Pattern:
    try:
        # Code that might fail
    except SpecificError:
        # Handle the error
    
  • Common File Errors:
    • FileNotFoundError - File doesn’t exist
    • json.JSONDecodeError - Invalid JSON format
    • PermissionError - Can’t read/write file
    • ValueError - Invalid data format
  • Graceful Degradation: Program continues even when files are missing/corrupted

🏛️ Programming Patterns

1. File Reading Pattern

try:
    with open("data.json", "r") as file:
        data = json.load(file)
except FileNotFoundError:
    data = default_value()

Used in: Loading flashcards and progress E.g.

try:
    with open("data/flashcards.json", "r") as file:
        flashcards = json.load(file)
except FileNotFoundError:
    flashcards = create_default_flashcards()

2. File Writing Pattern

with open("data.json", "w") as file:
    json.dump(data, file, indent=2)

Used in: Saving flashcards and progress E.g.

with open("data/flashcards.json", "w") as file:
    json.dump(flashcards, file, indent=2)

3. Append Pattern (for logs/history)

# Load existing data
try:
    with open("file.json", "r") as f:
        data_list = json.load(f)
except FileNotFoundError:
    data_list = []

# Add new item
data_list.append(new_item)

# Save everything back
with open("file.json", "w") as f:
    json.dump(data_list, f, indent=2)

Used in: Saving practice progress E.g.

# Load existing progress
try:
    with open("data/progress.json", "r") as file:
        progress_history = json.load(file)
except FileNotFoundError:
    progress_history = []

# Add new session
progress_history.append(progress_entry)

# Save updated history
with open("data/progress.json", "w") as file:
    json.dump(progress_history, file, indent=2)

📖 Important Terms

File I/O: Input/Output - reading from and writing to files.

JSON: JavaScript Object Notation - a text format for storing structured data.

Serialization: Converting Python objects (like dictionaries) into a format that can be stored in files.

Deserialization: Converting data from files back into Python objects, e.g. when the data in a json file are loaded as dictionaries.

Context Manager: The with statement that automatically manages resources (like closing files).

Exception: An error that occurs during program execution.

Try/Except Block: Code structure for handling exceptions gracefully.

Data Validation: Checking that data meets expected requirements before using it.

Persistence: The ability of data to survive beyond program execution.


Ready to Continue Your Journey?

Coming Soon - Extended Course materials under development

See outline