Editor

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 lesson

Write a function compare_to_budget(spending_totals, budget_limits) that:

  • Takes a dictionary of category spending totals and a dictionary of budget limits
  • Returns a list of dictionaries, each with keys: category, spent, budget, difference, status
  • difference 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

    All Python exercises · 50 practice problems · Practical Projects quiz