Module 24: Advanced Practice

Challenge yourself further with file writing techniques.

Build: Multiple Lines

Construct the code using `with open`. Write **two separate lines** to `report.txt` in append mode (`'a'`):
Line 1: `Status: OK\n`
Line 2: `Data logged.\n`

Drop pieces for the full 'with' block...

Challenge: Writing Numbers

The `.write()` method only accepts strings! Type code to write a score (e.g., `score = 100`) to `scores.txt` in write mode (`'w'`). Remember to convert the number to a string first using `str()`. Include a newline `\n`.

File content will appear here...

Write Using a Loop

Imagine you have a list: `items = ["CPU", "GPU", "RAM"]`. Write code using a `for` loop inside a `with open` block to write each item to `hardware.log` (append mode `'a'`), adding a newline `\n` after each item.

File content will appear here...

Advanced Mini Missions

Apply your skills to these challenges. Pay attention to filenames, modes, and required content format!

Adv. Mission 1: Single Write, Multiple Lines

Using only **one** `.write()` call, create/overwrite `greeting.txt` (mode `'w'`) with the exact content:
`Hello\nWorld\n`

Adv. Mission 2: Append Formatted Data

You have `user_id = 5` and `action = "updated"`. Append a line to `activity.log` (mode `'a'`) in the format: `ID: 5 - Action: updated\n`. Remember to convert `user_id` to a string!

Adv. Mission 3: Overwrite with Key-Value

Overwrite (`'w'`) the file `settings.ini` with the following key-value pairs, each on its own line (ending with `\n`):
`theme=dark`
`autosave=true`

Module 24 Advanced Practice Complete!

Excellent job tackling these advanced file writing exercises! You're building solid persistence skills.
Review the basic practice or watch the video lesson if needed, or head back to the course menu.