MCPcopy Create free account
hub / github.com/geekcomputers/Python / view_expenses

Function view_expenses

Personal-Expense-Tracker/expense_tracker.py:20–78  ·  view source on GitHub ↗
(expenses, period="all", category_filter=None)

Source from the content-addressed store, hash-verified

18
19
20def view_expenses(expenses, period="all", category_filter=None):
21 if not expenses:
22 print("No expenses recorded yet.")
23 return
24
25 filtered_expenses = expenses
26 if category_filter:
27 filtered_expenses = [
28 e for e in filtered_expenses if e["category"] == category_filter
29 ]
30
31 if period == "day":
32 date_str = input("Enter the date to view expenses for (YYYY-MM-DD): ")
33 try:
34 date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
35 filtered_expenses = [e for e in filtered_expenses if e["date"] == date]
36 except ValueError:
37 print("Incorrect date format.")
38 return
39 elif period == "week":
40 date_str = input(
41 "Enter the start date of the week (YYYY-MM-DD - first day of the week): "
42 )
43 try:
44 start_date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
45 end_date = start_date + datetime.timedelta(days=6)
46 filtered_expenses = [
47 e for e in filtered_expenses if start_date <= e["date"] <= end_date
48 ]
49 except ValueError:
50 print("Incorrect date format.")
51 return
52 elif period == "month":
53 year = input("Enter the year for the month (YYYY): ")
54 month = input("Enter the month (MM): ")
55 try:
56 year = int(year)
57 month = int(month)
58 filtered_expenses = [
59 e
60 for e in filtered_expenses
61 if e["date"].year == year and e["date"].month == month
62 ]
63 except ValueError:
64 print("Incorrect year or month format.")
65 return
66
67 if not filtered_expenses:
68 print("No expenses found for this period or category.")
69 return
70
71 print("\n--- Expenses ---")
72 total_spent = 0
73 for expense in filtered_expenses:
74 print(
75 f"Amount: {expense['amount']}, Category: {expense['category']}, Date: {expense['date']}, Note: {expense['note']}"
76 )
77 total_spent += expense["amount"]

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected