-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (52 loc) · 1.76 KB
/
app.py
File metadata and controls
61 lines (52 loc) · 1.76 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
import pprint
from datetime import datetime
import json
from pathlib import Path
def menu():
options = [
"0. Add an expense",
"1. View all expenses",
"2. Summary by Category",
"3. Quit",
]
for option in options:
print(option)
def main():
try:
with open("data.json", "r") as f:
expenses = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
expenses = {}
while True:
menu()
choice = int(input("Enter option number: "))
if choice >= 0 and choice <= 4:
if choice == 0:
while True:
category = input("Enter category for the expense: ")
if category.lower() == "q":
break
amount = float(input("Enter the amount of expense: "))
expenses.setdefault(category, []).append(
{"amount": amount, "date": datetime.now().isoformat()}
)
with open("data.json", "w") as f:
json.dump(expenses, f)
print(f"Expense added to {category}!")
break
elif choice == 1:
for category in expenses:
print(f"{category}:")
for index in range(len(expenses[category])):
print(expenses[category][index])
elif choice == 2:
for category in expenses:
total = 0
for item in expenses[category]:
total += item["amount"]
print(f"{category}: {total}$")
elif choice == 3:
exit()
else:
continue
main()