-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
115 lines (94 loc) · 3.26 KB
/
app.py
File metadata and controls
115 lines (94 loc) · 3.26 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
# Function to display the main menu
def show_menu():
print("===== Personal Expense Tracker =====")
print("1. Add an Expense")
print("2. View All Expenses")
print("3. Show Total Spending")
print("4. View Spending by Category")
print("5. Exit")
print("====================================")
# Function to add a new expense
def add_expense(expenses):
category = input("Enter category (Food, Travel, Shopping, etc.): ").capitalize()
amount = float(input("Enter amount spent: "))
note = input("Enter a short note (optional): ")
expense = {
"category": category,
"amount": amount,
"note": note
}
expenses.append(expense)
print(f"✅ Expense added: {category} - ₹{amount}")
# Function to view all expenses
def view_expenses(expenses):
# Check if there are no expenses yet
if len(expenses) == 0:
print("\nNo expenses recorded yet! Start by adding one.")
return
# If we have expenses, show them nicely
print("\n----- Your Expenses -----")
# Create a counter to keep track of numbering
count = 1
# Loop through each expense in the list
for expense in expenses:
category = expense["category"]
amount = expense["amount"]
note = expense["note"]
print(f"{count}. Category: {category} | Amount: {amount} | Note: {note}")
count += 1
# Function to show total spending
def total_spending(expenses):
# Check if there are no expenses yet
if len(expenses) == 0:
print("No expenses recorded yet! Add some first.")
return
# Start total from 0
total = 0
# Go through each expense and add the amount to total
for expense in expenses:
total = total + expense["amount"]
# Show the final total
print(f"\n💰 Total spending so far: ₹{total}")
# Function to view spending by category
def spending_by_category(expenses):
# Check if there are no expenses yet
if len(expenses) == 0:
print("\nNo expenses to analyze! Add some first.")
return
category_totals = {}
for expense in expenses:
category = expense['category']
amount = expense["amount"]
# If this category already exists, add to it
if category in category_totals:
category_totals[category] = category_totals[category] + amount
else:
# Otherwise, start a new category total
category_totals[category] = amount
print("\n📊 Spending by Category:")
for category, total in category_totals.items():
print(f"- {category}: {total}")
# -----------------------------
# Main Program
# -----------------------------
def main():
expenses = []
while True:
show_menu()
choice = input("Choose an option (1-5): ")
if choice == '1':
add_expense(expenses)
elif choice == '2':
view_expenses(expenses)
elif choice == '3':
total_spending(expenses)
elif choice == '4':
spending_by_category(expenses)
elif choice == '5':
print("👋 Exiting Expense Tracker. Have a great day!")
break
else:
print("❌ Invalid choice. Please try again.")
# Run the program
if __name__ == "__main__":
main()