"""Challenge 93: Improve a simple expense tracker."""

def total_expenses(expenses):
    return round(sum(amount for _, amount in expenses), 2)

def category_totals(expenses):
    totals = {}
    for category, amount in expenses:
        totals[category] = round(totals.get(category, 0) + amount, 2)
    return totals

def spending_warning(expenses, limit):
    return total_expenses(expenses) > limit

def main():
    expenses = [("Food", 12.50), ("Travel", 5.00), ("Food", 4.50)]
    limit = float(input("Spending limit (£): "))
    print(f"Total: £{total_expenses(expenses):.2f}")
    for category, amount in sorted(category_totals(expenses).items()):
        print(f"{category}: £{amount:.2f}")
    if spending_warning(expenses, limit):
        print("Warning: spending limit exceeded.")
    else:
        print("Within your spending limit.")

if __name__ == "__main__":
    main()
