-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_tracker.py
More file actions
146 lines (124 loc) · 4.68 KB
/
Copy pathexpense_tracker.py
File metadata and controls
146 lines (124 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import json
import os
from datetime import datetime
# Initialize an empty list to store expenses
expenses = []
# Create a data file to store expenses
data_file = "expenses.json"
# Load existing expenses from the data file, if it exists
if os.path.exists(data_file):
with open(data_file, "r") as file:
expenses = json.load(file)
# Function to add an expense
def add_expense():
name = input("Enter the name of the expense: ")
try:
amount = float(input("Enter the amount spent: "))
except ValueError:
print("Invalid amount. Please enter a valid number.")
return
category = input("Enter the category of the expense: ")
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
expenses.append({"date": date, "name": name, "amount": amount, "category": category})
with open(data_file, "w") as file:
json.dump(expenses, file)
print("Expense added successfully.")
# Function to generate an expense report
def generate_report():
if not expenses:
print("No expenses to generate a report.")
return
total_expenses = sum(expense["amount"] for expense in expenses)
print("\nExpense Report:")
print("-" * 50)
for expense in expenses:
print(f"Date: {expense['date']}")
print(f"Name: {expense['name']}")
print(f"Category: {expense['category']}")
print(f"Amount: Rs.{expense['amount']:.2f}")
print("-" * 50)
print(f"Total Expenses: Rs.{total_expenses:.2f}")
# Function to edit an expense
def edit_expense():
if not expenses:
print("No expenses to edit.")
return
print("Select an expense to edit:")
for i, expense in enumerate(expenses):
print(f"{i + 1}. {expense['name']} ({expense['category']}) - Rs.{expense['amount']:.2f}")
try:
choice = int(input("Enter the number of the expense to edit (0 to cancel): "))
if choice == 0:
return
if 1 <= choice <= len(expenses):
expense = expenses[choice - 1]
print("Editing Expense:")
print(f"Name: {expense['name']}")
print(f"Category: {expense['category']}")
print(f"Amount: Rs.{expense['amount']:.2f}")
new_name = input("Enter a new name (leave empty to keep the same): ")
new_category = input("Enter a new category (leave empty to keep the same): ")
new_amount = input("Enter a new amount (leave empty to keep the same): ")
if new_name:
expense['name'] = new_name
if new_category:
expense['category'] = new_category
if new_amount:
try:
expense['amount'] = float(new_amount)
except ValueError:
print("Invalid amount. Expense not updated.")
return
with open(data_file, "w") as file:
json.dump(expenses, file)
print("Expense updated successfully.")
else:
print("Invalid choice. Please select a valid expense to edit.")
except ValueError:
print("Invalid choice. Please enter a number.")
# Function to delete an expense
def delete_expense():
if not expenses:
print("No expenses to delete.")
return
print("Select an expense to delete:")
for i, expense in enumerate(expenses):
print(f"{i + 1}. {expense['name']} ({expense['category']}) - Rs.{expense['amount']:.2f}")
try:
choice = int(input("Enter the number of the expense to delete (0 to cancel): "))
if choice == 0:
return
if 1 <= choice <= len(expenses):
deleted_expense = expenses.pop(choice - 1)
with open(data_file, "w") as file:
json.dump(expenses, file)
print(f"{deleted_expense['name']} ({deleted_expense['category']}) - Rs.{deleted_expense['amount']:.2f} deleted.")
else:
print("Invalid choice. Please select a valid expense to delete.")
except ValueError:
print("Invalid choice. Please enter a number.")
# Function to show a menu of options
def show_menu():
print("\nExpense Tracker Menu:")
print("1. Add Expense")
print("2. Edit Expense")
print("3. Delete Expense")
print("4. Generate Expense Report")
print("5. Exit")
# Main program
while True:
show_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_expense()
elif choice == "2":
edit_expense()
elif choice == "3":
delete_expense()
elif choice == "4":
generate_report()
elif choice == "5":
print("Exiting Expense Tracker. Goodbye!")
break
else:
print("Invalid choice. Please try again.")