o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
give me a code for a budget manager in python
- User
give me a code for a budget manager in python
- Assistant
Here’s a simple, self-contained Python “Budget Manager” that lets you:
- Add income or expense entries (with date, category & notes)
- View a summary by category or overall
- Persist data in a JSON file
Feel free to extend it (e.g. add reports by date-range, CSV import/export, or a GUI).
#!/usr/bin/env python3 """ Simple CLI Budget Manager Stores transactions in a JSON file and lets you add/list/summarize. """ import json import os import datetime DATA_FILE = "budget_data.json" def load_data(): if not os.path.exists(DATA_FILE): return [] with open(DATA_FILE, "r") as f: return json.load(f) def save_data(data): with open(DATA_FILE, "w") as f: json.dump(data, f, indent=2, default=str) def add_transaction(data, t_type, amount, category, note=""): txn = { "type": t_type, # "income" or "expense" "amount": float(amount), "category": category, "note": note, "date": datetime.date.today().isoformat() } data.append(txn) save_data(data) print(f"✅ {t_type.title()} of {amount} added in '{category}'.") def list_transactions(data): if not data: print("⚠️ No transactions yet.") return print("\nTransactions:") for idx, t in enumerate(data, start=1): print(f"{idx:3}. [{t['date']}] {t['type']:7} | {t['category']:15} | " f"{t['amount']:8.2f} | {t['note']}") print() def summarize(data): total_inc = sum(t["amount"] for t in data if t["type"] == "income") total_exp = sum(t["amount"] for t in data if t["type"] == "expense") balance = total_inc - total_exp print("\n=== Summary ===") print(f"Total Income : {total_inc:8.2f}") print(f"Total Expense: {total_exp:8.2f}") print(f"Balance : {balance:8.2f}\n") # Breakdown by category by_cat = {} for t in data: key = (t["type"], t["category"]) by_cat.setdefault(key, 0) by_cat[key] += t["amount"] print("Breakdown by Category:") for (t_type, cat), amt in sorted(by_cat.items()): print(f" {t_type:7} | {cat:15} | {amt:8.2f}") print() def prompt_menu(): print(""" Budget Manager ============== 1) Add Income 2) Add Expense 3) List All Transactions 4) Show Summary 5) Quit """) def main(): data = load_data() while True: prompt_menu() choice = input("Select an option [1-5]: ").strip() if choice == "1": amt = input("Amount : ") cat = input("Category : ") note = input("Note : ") add_transaction(data, "income", amt, cat, note) elif choice == "2": amt = input("Amount : ") cat = input("Category : ") note = input("Note : ") add_transaction(data, "expense", amt, cat, note) elif choice == "3": list_transactions(data) elif choice == "4": summarize(data) elif choice == "5": print("Goodbye 👋") break else: print("❗ Invalid choice. Please select 1–5.") if __name__ == "__main__": main()How to use:
- Save the above to
budget_manager.py - Run in your shell:
python3 budget_manager.py - A file
budget_data.jsonwill be created alongside, storing your entries.
Next steps / Ideas for improvement:
- Filter transactions by date range
- Import/Export CSV
- Connect to a real database (SQLite)
- Build a simple web or desktop UI (Flask, Tkinter, etc.)
Let me know if you’d like any of these features fleshed out! 😊
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.