Budget Comparison
Intermediate · Write the code · from Build a Budget Tracker and Expense Analyzer
From the lesson: Step 4: Compare Spending to Budget Limits
Having spending totals is useful, but the real insight comes from comparing them to your budget. Are you over or under in each category? By how much? This is where the analysis becomes actionable.
Read this part of the lessonWrite a function compare_to_budget(spending_totals, budget_limits) that:
category, spent, budget, difference, statusdifference is budget - spent (positive = under budget, negative = over)status is "Under" if difference >= 0, else "OVER"Print each category's status.
import pandas as pd
def compare_to_budget(spending_totals, budget_limits):
# Your code here
pass
spending = {'Food': 450.00, 'Utilities': 255.00, 'Transport': 120.00,
'Entertainment': 139.99, 'Health': 52.50}
budget = {'Food': 400.00, 'Utilities': 300.00, 'Transport': 150.00,
'Entertainment': 100.00, 'Health': 75.00}
results = compare_to_budget(spending, budget)
for r in results:
print(f'{r["category"]}: ${r["spent"]:.2f} / ${r["budget"]:.2f} [{r["status"]}]')
More Budget Tracker exercises
- Create the Expense DataFrameIntermediate
- Categorize ExpensesIntermediate
- Spending Totals by CategoryIntermediate
- Identify OverspendingIntermediate
- Complete Budget ReportIntermediate
All Python exercises · 50 practice problems · Practical Projects quiz