-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonal-expense-tracker.py
More file actions
185 lines (158 loc) · 5.87 KB
/
personal-expense-tracker.py
File metadata and controls
185 lines (158 loc) · 5.87 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# PERSONAL EXPENSE TRACKER
# This program allows users to track their personal expenses.
expenses = []
expense_id = 1 # Unique ID for each expense
# Display the menu once at the start
print("\nPERSONAL EXPENSE TRACKER")
print("1. Add Expense")
print("2. View Expenses")
print("3. Delete Expense")
print("4. Update Expense")
print("5. Search Expense by ID")
print("6. Summary")
print("7. Exit")
print("Type 'menu' to display the menu again.")
# Main Menu
while True:
choice = input("\nEnter your choice: ").strip().lower()
# Display the menu again if the user types 'menu'
if choice == 'menu':
print("\nPERSONAL EXPENSE TRACKER")
print("1. Add Expense")
print("2. View Expenses")
print("3. Delete Expense")
print("4. Update Expense")
print("5. Search Expense by ID")
print("6. Summary")
print("7. Exit")
print("Type 'menu' to display the menu again.")
continue
# Add Expense
if choice == '1':
try:
amount = float(input("ENTER AMOUNT OF YOUR EXPENSE: "))
except ValueError:
print("INVALID AMOUNT! PLEASE ENTER A NUMBER.")
continue
category = input("ENTER CATEGORY OF YOUR EXPENSE: ").strip()
if not category:
print("CATEGORY CANNOT BE EMPTY!")
continue
description = input("ENTER DESCRIPTION OF YOUR EXPENSE: ").strip()
if not description:
print("DESCRIPTION CANNOT BE EMPTY!")
continue
expenses.append({
'id': expense_id,
'amount': amount,
'category': category,
'description': description
})
expense_id += 1
print("YOUR EXPENSE HAS BEEN ADDED SUCCESSFULLY!")
# View Expenses
elif choice == '2':
if not expenses:
print("NO EXPENSES TO BE FOUND!!!")
continue
print(f"{'ID':<7} {'AMOUNT':<15} {'CATEGORY':<25} {'DESCRIPTION':<35}")
print("-" * 65)
for i in expenses:
print(f"{i['id']:<7} {i['amount']:<15.2f} {i['category']:<25} {i['description']:<35}")
# Delete Expense
elif choice == '3':
try:
del_id = int(input("ENTER THE EXPENSE ID YOU WANTED TO DELETE: "))
except ValueError:
print("INVALID ID! PLEASE ENTER A VALID NUMBER.")
continue
found = False
for i in expenses:
if i['id'] == del_id:
confirm = input(f"ARE YOU SURE YOU WANT TO DELETE EXPENSE ID {del_id}? (yes/no): ").strip().lower()
if confirm == 'yes':
expenses.remove(i)
print("YOUR EXPENSE HAS BEEN DELETED SUCCESSFULLY!")
else:
print("DELETION CANCELLED.")
found = True
break
if not found:
print("INVALID EXPENSE ID!!!")
# Update Expense
elif choice == '4':
if not expenses:
print("NO EXPENSES TO UPDATE!!!")
continue
try:
update_id = int(input("ENTER THE EXPENSE ID YOU WANTED TO UPDATE: "))
except ValueError:
print("INVALID ID! PLEASE ENTER A VALID NUMBER.")
continue
found = False
for i in expenses:
if i['id'] == update_id:
print("YOUR EXPENSE IS AS FOLLOWS:")
print(f"ID: {i['id']}, AMOUNT: {i['amount']}, CATEGORY: {i['category']}, DESCRIPTION: {i['description']}")
print("-------------------------------")
try:
amount = float(input("ENTER NEW AMOUNT OF YOUR EXPENSE: "))
except ValueError:
print("INVALID AMOUNT! PLEASE ENTER A NUMBER.")
continue
category = input("ENTER NEW CATEGORY OF YOUR EXPENSE: ").strip()
if not category:
print("CATEGORY CANNOT BE EMPTY!")
continue
description = input("ENTER NEW DESCRIPTION OF YOUR EXPENSE: ").strip()
if not description:
print("DESCRIPTION CANNOT BE EMPTY!")
continue
i['amount'] = amount
i['category'] = category
i['description'] = description
print("YOUR EXPENSE HAS BEEN UPDATED SUCCESSFULLY!")
found = True
break
if not found:
print("INVALID EXPENSE ID!!!")
# Search Expense by ID
elif choice == '5':
try:
search_id = int(input("ENTER THE EXPENSE ID YOU WANT TO SEARCH FOR: "))
except ValueError:
print("INVALID ID! PLEASE ENTER A VALID NUMBER.")
continue
found = False
for i in expenses:
if i['id'] == search_id:
print("YOUR EXPENSE IS AS FOLLOWS:")
print(f"ID: {i['id']}, AMOUNT: {i['amount']}, CATEGORY: {i['category']}, DESCRIPTION: {i['description']}")
found = True
break
if not found:
print("INVALID EXPENSE ID!!!")
# Summary
elif choice == '6':
if not expenses:
print("NO EXPENSES TO SUMMARIZE!!!")
continue
summary = {}
for i in expenses:
category = i['category']
summary[category] = summary.get(category, 0) + i['amount']
print(f"{'CATEGORY':<25} {'TOTAL AMOUNT':<15}")
print("-" * 40)
for category, total in summary.items():
print(f"{category:<25} {total:<15.2f}")
# Exit
elif choice == '7':
confirm = input("ARE YOU SURE YOU WANT TO EXIT? (yes/no): ").strip().lower()
if confirm == 'yes':
print("Exiting... Goodbye!")
break
else:
print("EXIT CANCELLED.")
# Invalid Choice
else:
print("INVALID CHOICE! PLEASE TRY AGAIN.")