๐ Introduction

In this project, youโll create a command-line Expense Tracker that allows users to:
โ
Add expenses with category, amount, and description
โ
View total spending and category breakdown
โ
Automatically save data to a JSON file
โ
Load and update expense history every time the program runs
This project applies file handling, dictionaries, JSON, loops, and user input.
๐ง Key Concepts Covered
| Concept | Description |
|---|---|
| JSON Storage | Save and load user data permanently |
| Dictionaries & Lists | Store expense details cleanly |
| Functions | Modular, reusable code structure |
| Data Reports | Calculate totals and category summaries |
| File Handling | Read/write data to disk |
๐งฐ Requirements
- Python 3 installed
- Basic knowledge of lists, dictionaries, JSON
- Text editor (VS Code, Notepad++, etc.)
๐ File Name
expense_tracker.py
๐ป Full Code
import json
import os
DATA_FILE = "expenses.json"
def load_expenses():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as file:
return json.load(file)
return []
def save_expenses(expenses):
with open(DATA_FILE, "w") as file:
json.dump(expenses, file, indent=4)
def add_expense(expenses):
amount = float(input("๐ต Enter amount: "))
category = input("๐ Enter category (Food, Transport, etc.): ")
description = input("๐ Optional note/description: ")
expenses.append({"amount": amount, "category": category, "desc": description})
save_expenses(expenses)
print("โ
Expense added successfully!\n")
def view_summary(expenses):
if not expenses:
print("โ ๏ธ No expenses recorded yet.\n")
return
total = sum(item["amount"] for item in expenses)
print(f"\n๐ Total Spending: ${total:.2f}")
category_totals = {}
for item in expenses:
category_totals[item["category"]] = category_totals.get(item["category"], 0) + item["amount"]
print("\n๐ Spending by Category:")
for cat, amt in category_totals.items():
print(f" - {cat}: ${amt:.2f}")
print()
def list_expenses(expenses):
if not expenses:
print("โ ๏ธ No expenses found.\n")
return
print("\n๐งพ Expense History:")
for i, item in enumerate(expenses, 1):
print(f"{i}. ${item['amount']:.2f} - {item['category']} ({item['desc']})")
print()
def main():
expenses = load_expenses()
while True:
print("๐ฐ Expense Tracker App")
print("1. Add Expense")
print("2. View Summary")
print("3. List All Expenses")
print("4. Exit")
choice = input("๐ Choose an option (1-4): ")
if choice == "1":
add_expense(expenses)
elif choice == "2":
view_summary(expenses)
elif choice == "3":
list_expenses(expenses)
elif choice == "4":
print("๐ Goodbye! Data saved.")
break
else:
print("โ Invalid choice, try again.\n")
if __name__ == "__main__":
main()
๐งช Example Output
๐ฐ Expense Tracker App
1. Add Expense
2. View Summary
3. List All Expenses
4. Exit
๐ Choose an option (1-4): 1
๐ต Enter amount: 12.50
๐ Enter category (Food, Transport, etc.): Food
๐ Optional note/description: Burger and fries
โ
Expense added successfully!
๐ Choose an option (1-4): 2
๐ Total Spending: $12.50
๐ Spending by Category:
- Food: $12.50
๐ Bonus Extensions
| Upgrade | How |
|---|---|
| โ Export report to CSV | Use csv module |
| โ GUI version | Use Tkinter or PyQt5 |
| โ Date support | Add datetime.now() |
| โ Monthly report | Filter by date range |
| โ Pie chart of spending | Use Matplotlib |
| โ Convert to Android APK | Using Kivy |
๐ง Summary
โ You created a real-world expense tracker
โ Data is saved permanently using JSON
โ You practiced file I/O, functions, loops, validation
โ You now have a mini personal finance tool!