Build a command-line expense tracker
Build a command-line expense tracker
Create a menu-driven expense tracker that accepts a description, amount and category. Store records in JSON or CSV, calculate totals by category and handle invalid input. Separate input/output from calculation functions so the logic can be tested.
Try it yourself
Change the example, run it, and explain the result in your own words. Then create one variation of your own.
Key idea
Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.
Code example
expenses = []
def add_expense(description, amount):
if amount <= 0:
raise ValueError("Amount must be positive")
expenses.append({"description": description, "amount": amount})
add_expense("Internet", 12000)
print(sum(item["amount"] for item in expenses))