-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (86 loc) · 3.47 KB
/
main.py
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
ACTION_PROMPT = "Enter (1) to add an expense | (2) to view expenses | (3) to delete an expense | or (4) to reset your expenses: "
DEL_CATEGORY_PROMPT = "Enter the Category you want to delete from: "
def greeting():
print("""Welcome to Expense Tracker! Expense Tracker is designed to keep track of all your expenses.
You can add expenses, delete expenses, view expenses, and clear all your expenses!\n""")
expenses = {}
def select_action():
action = input(ACTION_PROMPT)
while action not in ["1", "2", "3", "4"]:
print("Invalid input")
action = input(ACTION_PROMPT)
return action
def adding_expense():
while True:
category = input("Enter the category of the expense (e.g, food, transportation, entertainment, E.T.C): ").capitalize().strip()
while True:
try:
value = float(input("Enter the expense value: "))
break
except ValueError:
print("Invalid input")
if category not in expenses:
expenses[category] = [value]
else:
expenses[category].append(value)
choice = input("Enter (y) if you would like to add another expense or enter (n) to exit: ").lower()
if choice != "y":
break
def show_expenses():
if not expenses:
print("Your expense tracker is empty.")
return
for category, entries in expenses.items():
print(f"Category: {category}")
for value in entries:
print(f" ${value}")
def delete_expense():
show_expenses()
category = input(DEL_CATEGORY_PROMPT)
while category not in expenses:
print("Invalid input, category not found")
category = input(DEL_CATEGORY_PROMPT)
while True:
try:
value = float(input(f"Enter the value you want to delete from {category}: "))
if value not in expenses[category]:
print(f"Invalid input, value not found in {category}")
else:
break
except ValueError:
print("Invalid input, please enter a numeric value")
print(f"Deleted {value} from {category}")
expenses[category].remove(value)
if not expenses[category]:
del expenses[category]
print(f"Deleted empty category: {category}")
def reset_all_expenses():
print("All of your expenses have been cleared")
expenses.clear()
def main(action):
if action == "1":
adding_expense()
elif action == "2":
show_expenses()
elif action == "3":
delete_expense()
elif action == "4":
reset_all_expenses()
def summary():
print("\nThank you for using the expense tracker!")
if expenses:
print("Here is a summary of your total expenses per category:\n")
for category in expenses.keys():
total = sum(expenses[category])
print(f"{category} total: ${total}")
else:
print("You don't have any expenses!")
print(f"Your most expensive purchase was ${max(max(expenses.values()))}")
greeting()
while True:
action = select_action()
main(action)
program_control = input("Enter (y) to continue using the expense tracker or (e) to exit the program: ").lower()
if program_control == "e":
summary()
break