Repository navigation
Expand file tree
/
Copy pathexpense.py
More file actions
143 lines (107 loc) · 3.23 KB
/
Copy pathexpense.py
File metadata and controls
143 lines (107 loc) · 3.23 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
import json
import os
FILE_NAME = "expenses.json"
def load_expenses():
if not os.path.exists(FILE_NAME):
return []
try:
with open(FILE_NAME, "r") as file:
return json.load(file)
except (json.JSONDecodeError, FileNotFoundError):
return []
def save_expenses(expenses):
with open(FILE_NAME, "w") as file:
json.dump(expenses, file, ident=4)
def add_expense(expenses):
category = input("Enter category: ").strip()
description = input("Enter description: ").strip()
try:
amount = float(input("Enter amount: "))
except ValueError:
print("Invalid amount.")
return
if amount <= 0:
print("Amount must be greater than zero.")
return
expense = {
"category": category,
"description": description,
"amount": amount
}
expenses.append(expenses)
save_expenses(expenses)
print("Expense added successfully.")
def show_expenses(expenses):
if not expenses:
print("No expenses recored.")
return
print("\n--- All Expenses ---")
for index, expense in enumerate(expenses, start=1):
print(
f"{index}."
f"{expense['category']} | "
f"{expense['description']} |"
f"₹{expense['amount']:.2f}"
)
def show_total(expenses):
total = sum(expense["amount"] for expense in expenses)
print(f"\nTotal expenses: ₹{total:.2f}")
def show_category_summary(expenses):
if not expenses:
print("No expenses recorded.")
return
summary = {}
for expense in expenses:
category = expense["category"]
summary[category] = summary.get(category, 0) + expense["amount"]
print("\n--- Category Summary ---")
for category, amount in summary.items():
print(f"{category}: ₹{amount:.2f}")
def delete_expense(expenses):
show_expenses(expenses)
if not expenses:
return
try:
index = int(input("\nEnter expense number to delete: "))
except ValueError:
print("Invalid number. ")
return
if index < 1 or index > len(expenses):
print("Invalid number.")
return
removed = expenses.pop(index - 1)
save_expenses(expenses)
print(
f"Deleted: {removed['description']}"
f"(₹{removed['amount']:.2f})"
)
def main():
expenses = load_expenses()
while True:
print("\n================")
print(" EXPENSE TRACKER")
print("===================")
print("1. Add expense")
print("2. Show expenses")
print("3. Show total")
print("4. Category summary")
print("5. Delete expense")
print("6. Exit")
choice = input("\nChoose an option: ").strip()
if choice == "1":
add_expense(expenses)
elif choice == "2":
show_expenses(expenses)
elif choice == "3":
show_total(expenses)
elif choice == "4":
show_category_summary(expenses)
elif choice == "5":
delete_expense(expenses)
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()