Lab: M3: functions, files, libraries & errors
You'll need: your own laptop and its terminal. This is your first local install: set aside time and go slowly. Time: ~60 min • Work in your breakout pair.
Heads up: installing software is where everyone hits a snag, it is not a sign you're behind. We go one tiny step at a time, separately for each operating system. Nothing here can harm your computer. If a step doesn't match your screen, check the troubleshooting table in
resources/install-guides/python-venv.md.
This lab has two parts: - Part A: set up Python, a virtual environment, and your first library. - Part B: write reusable functions that read a file, save JSON, and survive a bad line.
flowchart LR
F["expenses.txt<br/>(a file you read)"] --> P["functions transform it<br/>(categorize each)"]
P --> J["expenses_categorized.json<br/>(a file you write)"]
P -.bad line?.-> E["try / except<br/>skips it safely"]
Part A: set up Python on your machine
Use
pythonon Windows andpython3on macOS/Linux. The commands below show macOS/Linux; Windows differences are noted. Full per-OS detail: the install guide.
Step 1: Open your terminal
Open Terminal (macOS/Linux) or PowerShell (Windows). New to it? See terminal.md.
You should now see: a window with a prompt ending in $ (macOS/Linux) or > (Windows) and a
blinking cursor.
Step 2: Install Python and check the version
Install Python 3.10+ following the install guide, Step 1 for your OS. Then, in a new terminal:
python3 --version # Windows: python --version
Python 3.12.x (any 3.10 or newer). If not, see the guide's
Troubleshooting table, don't push on until this works.
Step 3: Make a project folder
mkdir ai-course && cd ai-course # Windows: mkdir ai-course; cd ai-course
ai-course. You're inside the project folder.
Step 4: Create a virtual environment
A virtual environment is a private box for this project's libraries.
python3 -m venv .venv # Windows: python -m venv .venv
.venv folder now exists in ai-course. (You
create this once.)
Step 5: Activate it
source .venv/bin/activate # Windows PowerShell: .\.venv\Scripts\Activate.ps1
(.venv). That means the box is active.
(Re-run this whenever you open a new terminal. Windows "scripts disabled" error? See the guide.)
Step 6: Install your first library
pip install rich
Successfully installed rich-15.x …. You just installed
someone else's code to reuse, that's pip.
Step 7: Confirm it's there
pip list
rich (e.g. rich 15.0.0). Setup
done. Take a breath, the hard part is behind you.
Part B: reusable code that survives the real world
Step 8: Get the starter files
Put budget_pro_starter.py and expenses.txt (from this module's
starters/ folder, your instructor will share them) into your ai-course
folder, next to .venv.
You should now see: running ls (Windows: dir) lists budget_pro_starter.py,
expenses.txt, and .venv.
Step 9: Run it once and watch errors get handled
With your venv active:
python budget_pro_starter.py # macOS/Linux may need python3
Skipping bad line: 'this line is broken on purpose', then each
expense printed with -> None. Two things just happened: it read the file, and the deliberately
broken line didn't crash the program: try/except caught it and skipped it. (The sizes are
None because we haven't filled in the function yet, next step.)
Step 10: Finish the categorize() function
Open budget_pro_starter.py in any editor. Find categorize() and replace the two "____" blanks
with "medium" and "big". Save, then run it again.
python budget_pro_starter.py
Coffee: $4.50 -> small,
Groceries: $62.00 -> big. A function is a named, reusable block, you call categorize(...)
once per expense and it hands back an answer.
Step 11: Save the results as JSON
In the same file, find the # TODO: save the list ... comment near the bottom and replace the
print("Done ...") placeholder with the two lines shown in the hint:
with open("expenses_categorized.json", "w") as f:
json.dump(expenses, f, indent=2)
You should now see: a new file expenses_categorized.json in your folder. Open it, it's
your list of dictionaries written as JSON text. This is the exact format an AI API speaks
(M4). You just wrote data to disk.
Step 12: See the library pay off (the full solution)
Now run the finished version, which uses rich to draw a table:
python ../path/to/solution/budget_pro.py # or copy solution/budget_pro.py into your folder and run it
solution/budget_pro.py into ai-course next to expenses.txt, then
python budget_pro.py.)
You should now see: a neat bordered table of your expenses with colours, a bold total, and
a green Saved expenses_categorized.json. That polish is the rich library doing the work, code
you installed, not code you wrote.
Step 13: Make it yours
Edit expenses.txt to your real spending (keep the item, amount shape), then re-run.
You should now see: your own expenses categorized, totalled, and saved. You didn't touch the functions, they handle whatever the file contains. That's reusable code.
Stuck on the code? The finished, commented version is in
../solution/budget_pro.py. Peek only after you've tried.
Your win
You set up Python locally, installed and used a library, organized your code into functions, read a file, saved JSON, and handled a bad line without crashing, the exact toolkit Part B's AI API needs.
Post it to the chat wins board: a screenshot of your rich table, or
"Python's running on my machine, I read a file, saved JSON, and a broken line didn't even slow me down "
Take-home (optional)
Add a new function count_big(expenses) that returns how many expenses are "big", and print it.
One small function, reusing categorize(). (Hint: loop and count, like M2's big_count.)