-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpensetracker.py
More file actions
72 lines (55 loc) · 1.71 KB
/
expensetracker.py
File metadata and controls
72 lines (55 loc) · 1.71 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
#declaring an empty list
expenses = []
#function to add expense
def add_expense():
try:
amount = float(input("Please enter the expense: \n"))
expenses.append(amount)
print("Expense added successfully!\n")
except ValueError:
print("Invalid input. Please enter the valid input!\n")
#function to view expenses
def view_expenses():
if len(expenses) == 0:
print("No expenses recorded\n")
else:
print("Your expenses are: ")
for index, amt in enumerate(expenses, 1):
print(f"{index} {amt}")
#function to calculate total and average
def total_avg_expenses():
if len(expenses) == 0:
print("No expenses are recorded\n")
else:
total = sum(expenses)
avg = total/len(expenses)
print(f"Total expense is: {total}")
print(f"Average of expenses is: {avg:.2f}\n")
#function to clear all expenses
def clear_expenses():
expenses.clear()
print("All expenses are cleared!\n")
#function to display menu
def menu():
while(True):
print("***Menu***")
print("1. Add expense")
print("2. View all expense")
print("3. Total and Average of expenses")
print("4. Clear all expenses")
print("5. Exit")
choice = input("Please enter the choice: ")
if choice == "1":
add_expense()
elif choice == "2":
view_expenses()
elif choice == "3":
total_avg_expenses()
elif choice == "4":
clear_expenses()
elif choice == "5":
print("Exiting")
break
else:
print("Inavlid choice! Please try again.")
menu()