-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject2.py
More file actions
58 lines (46 loc) · 1.69 KB
/
project2.py
File metadata and controls
58 lines (46 loc) · 1.69 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
import csv
expensesList = []
print("Welcome to Expense Tracker (CSV Version)")
file_name = "expenses.csv" # save data in csv file
while True:
print("\n==== MENU ====")
print("1. Add Expense")
print("2. View All Expenses")
print("3. View Total Spending")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
date = input("Enter expense date: ")
category = input("Enter category: ")
description = input("Enter description: ")
amount = float(input("Enter amount: "))
expense = [date, category, description, amount]
with open(file_name, "a", newline="") as file:
writer = csv.writer(file)
writer.writerow(expense)
print("Expense saved to file successfully.")
elif choice == 2:
try:
with open(file_name, "r") as file:
reader = csv.reader(file)
print("\nDate | Category | Description | Amount")
print("--------------------------------------")
for row in reader:
print(row)
except FileNotFoundError:
print("No expense file found.")
elif choice == 3:
total = 0
try:
with open(file_name, "r") as file:
reader = csv.reader(file)
for row in reader:
total += float(row[3])
print(f"\nTotal Spending: {total}")
except FileNotFoundError:
print("No expenses recorded yet.")
elif choice == 4:
print("Exiting system. Have a nice day!")
break
else:
print("Invalid choice.")