Djoker_tv

๐Ÿงพ Project Overview

In this project, you will build a Python Expense Tracker App that allows users to:

The current image has no alternative text. The file name is: IMG_20250829_023848_edit_182322646459158-scaled.jpg

โœ” Add new expenses
โœ” View all expenses
โœ” Search by category
โœ” Calculate total spending
โœ” Save all data automatically using a JSON file
โœ” Provide a clean menu-based interface

This project teaches:

  • JSON handling
  • File operations
  • Lists & dictionaries
  • Functions
  • Loops and user input

Final Output: A fully functional console application that stores expenses permanently.


๐Ÿงฑ 1. Data Structure

Each expense will be stored as a dictionary:

{
    "amount": 25.5,
    "category": "Food",
    "note": "Lunch",
    "date": "2025-01-12"
}

All expenses are stored inside expenses.json as a list.


๐Ÿ“ฆ 2. Full Python Code (expense_tracker.py)

๐Ÿ‘‰ Create a file named:

expense_tracker.py

๐Ÿ‘‰ Create also an empty JSON file:

expenses.json

Now paste this full code:

import json
import os
from datetime import datetime

# Load existing expenses from JSON file
def load_expenses():
    if os.path.exists("expenses.json"):
        with open("expenses.json", "r") as file:
            try:
                return json.load(file)
            except json.JSONDecodeError:
                return []
    return []

# Save expenses to JSON file
def save_expenses(expenses):
    with open("expenses.json", "w") as file:
        json.dump(expenses, file, indent=4)

# Add a new expense
def add_expense(expenses):
    amount = float(input("Enter amount: "))
    category = input("Enter category (Food, Travel, Bills...): ").capitalize()
    note = input("Enter a short note: ")
    date = datetime.now().strftime("%Y-%m-%d")

    expense = {
        "amount": amount,
        "category": category,
        "note": note,
        "date": date
    }

    expenses.append(expense)
    save_expenses(expenses)
    print("โœ” Expense added successfully!\n")

# View all expenses
def view_expenses(expenses):
    if not expenses:
        print("No expenses found.\n")
        return

    print("\n๐Ÿ“œ All Expenses:")
    for i, exp in enumerate(expenses, start=1):
        print(f"{i}. {exp['date']} | ${exp['amount']} | {exp['category']} โ€“ {exp['note']}")
    print()

# Search expenses by category
def search_by_category(expenses):
    cat = input("Enter category to search: ").capitalize()
    results = [exp for exp in expenses if exp["category"] == cat]

    if not results:
        print("No expenses found for this category.\n")
        return

    print(f"\n๐Ÿ”Ž Expenses in '{cat}':")
    for exp in results:
        print(f"- {exp['date']} | ${exp['amount']} | {exp['note']}")
    print()

# Calculate total spending
def total_spent(expenses):
    total = sum(exp["amount"] for exp in expenses)
    print(f"\n๐Ÿ’ฐ Total spending: ${total}\n")

# Main menu
def main():
    expenses = load_expenses()

    while True:
        print("๐Ÿ“Š Expense Tracker App")
        print("1. Add Expense")
        print("2. View All Expenses")
        print("3. Search by Category")
        print("4. Total Spending")
        print("5. Exit")
        
        choice = input("๐Ÿ‘‰ Choose an option (1-5): ")

        if choice == "1":
            add_expense(expenses)
        elif choice == "2":
            view_expenses(expenses)
        elif choice == "3":
            search_by_category(expenses)
        elif choice == "4":
            total_spent(expenses)
        elif choice == "5":
            print("Goodbye!")
            break
        else:
            print("โŒ Invalid option. Try again.\n")

if __name__ == "__main__":
    main()

๐Ÿ–ฅ 3. How to Run the Program

  1. Save both files in the same folder:
    • expense_tracker.py
    • expenses.json (empty file)
  2. Open CMD in that folder.
  3. Run:
python expense_tracker.py

You will see:

๐Ÿ“Š Expense Tracker App
1. Add Expense
2. View All Expenses
3. Search by Category
4. Total Spending
5. Exit
๐Ÿ‘‰ Choose an option (1-5):

๐Ÿ“Œ 4. Example Usage

โž• Add Expense:

Enter amount: 12.5
Enter category: Food
Enter note: Sandwich
โœ” Expense added successfully!
๐Ÿ“œ View All:
1. 2025-01-12 | $12.5 | Food โ€“ Sandwich
๐Ÿ” Search Category:
Enter category: Food
- 2025-01-12 | $12.5 | Sandwich
๐Ÿ’ฐ Total Spending:
Total spending: $12.5

๐Ÿง  5. Summary
FeatureDescription
Add ExpenseAdd amount, category, note, date
View ExpensesList all recorded expenses
Category SearchFilter by category
Total SpendingAuto-calculates all expenses
JSON StorageSaves data permanently

๐ŸŽ‰ Project Completed!

This is a real, practical Python app that can evolve into:

โœ” GUI version (Tkinter)
โœ” Mobile app (Kivy)
โœ” Dashboard with charts (Matplotlib)
โœ” Cloud-based version

โฌ‡๏ธ Download Joker ุชุญู…ูŠู„ Server 3

Related Posts

Fiveg_Tv

๐Ÿ” Project Overview In this project, you will create a secure Password Manager that : โœ” Saves usernames & passwordsโœ” Encrypts all dataโœ” Stores everything in a JSON fileโœ” Allows…

Read more

FlyFeditv

๐Ÿ“˜ 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…

Read more

NetCinFly

๐Ÿ“˜ Introduction In this project, youโ€™ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

๐ŸŒ Introduction In this project, youโ€™ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.Youโ€™ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

๐ŸŒฆ๏ธ Introduction In this project, youโ€™ll build a Weather App in Python that retrieves live weather information from an online API.Youโ€™ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: ๐ŸŽฏ Objective: Create a simple, text-based contact book application that allows users to: ๐Ÿ’ก Code (contact_book.py): ๐Ÿง  Example Usage Console Output Example: ๐Ÿ’พ Notes โฌ‡๏ธ Download Show7-Pro ุชุญู…ูŠู„…

Read more