Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Expense-Tracker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Expense Tracker

A simple command-line expense tracker built with Python.

## Features

- Add expenses
- View all expenses
- Calculate total expenses
- Delete expenses
- Save expenses using JSON
- Load previous expenses when the program starts

## Requirements

- Python 3.x

## How to Run

Navigate to the `Expense-Tracker` directory and run:

```bash
python expense_tracker.py

## if the python command is not recognised on your system, you can use the Python launcher instead:

py expense_tracker.py


##you can also check whether the python is installed or not using:
py --version


##About Data storage expenses are stored locally in expenses.json. The file is automatiocally updated whenever an expense is added or deleted.
133 changes: 133 additions & 0 deletions Expense-Tracker/expense_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import json

FILE_NAME = "expenses.json"


def load_expenses():
try:
with open(FILE_NAME, "r") as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return []


def save_expenses():
with open(FILE_NAME, "w") as file:
json.dump(expenses, file, indent=4)


def add_expense():
while True:
try:
amount = float(input("Enter amount: "))

if amount <= 0:
print("Amount must be greater than 0.")
continue

break

except ValueError:
print("Please enter a valid number.")

category = input("Enter category: ").strip()
description = input("Enter description: ").strip()

expense = {
"amount": amount,
"category": category,
"description": description
}

expenses.append(expense)
save_expenses()

print("Expense added successfully!")


def view_expenses():
if not expenses:
print("No expenses recorded.")
return

print("\n--- Expenses ---")

for index, expense in enumerate(expenses, start=1):
print(
f"{index}. "
f"₹{expense['amount']:.2f} | "
f"{expense['category']} | "
f"{expense['description']}"
)


def view_total():
total = sum(expense["amount"] for expense in expenses)
print(f"\nTotal Expenses: ₹{total:.2f}")


def delete_expense():
if not expenses:
print("No expenses to delete.")
return

view_expenses()

while True:
try:
choice = int(input("Enter expense number to delete: "))

if 1 <= choice <= len(expenses):
deleted_expense = expenses.pop(choice - 1)
save_expenses()

print(
f"Deleted: {deleted_expense['category']} - "
f"₹{deleted_expense['amount']:.2f}"
)
break

print("Invalid expense number.")

except ValueError:
print("Please enter a valid number.")


def main():
global expenses

expenses = load_expenses()

while True:
print("\n===== Expense Tracker =====")
print("1. Add Expense")
print("2. View Expenses")
print("3. View Total")
print("4. Delete Expense")
print("5. Exit")

choice = input("Enter your choice: ").strip()

if choice == "1":
add_expense()

elif choice == "2":
view_expenses()

elif choice == "3":
view_total()

elif choice == "4":
delete_expense()

elif choice == "5":
print("Goodbye!")
break

else:
print("Invalid choice. Please select 1-5.")



if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions Expense-Tracker/expenses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]