Lab, Practical Projects
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write a class Expense whose __init__(self, day, category, amount) stores the three values (with day converted from ISO text to a date), and whose __repr__ returns text like Expense(2024-03-02, food, 23.40). Print a list holding two expenses.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so by_category adds each amount to its category's running total.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program saves two expenses and loads them back, and should print Total: 30.75. It crashes while loading. Fix the loading loop.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows stored and rejected each time one of them changes, once both exist.
CATEGORIES = {"food", "fun"}
stored = 0
rejected = 0
for category, amount in [("food", 5), ("pets", 3), ("fun", -2), ("fun", 4)]:
if category in CATEGORIES and amount > 0:
stored = stored + 1
else:
rejected = rejected + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Define one method by(self, key) and call it with lambdas; no by_category or by_month methods.
This code works, but by_category and by_month repeat the same loop. Replace them with one method by(self, key) that takes a key function, and call it with two lambdas. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write ExpenseError and a Tracker with add(day_text, category, amount) (raise ExpenseError for an unknown category from {"food", "home", "fun"} or an amount that is not positive), total(), save(path) and load(path) using CSV with a header date,category,amount. Run the starter steps so the output is:
rejected: unknown category 'pets' rejected: amount must be positive saved 3, total 95.5 loaded 3, total 95.5
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.