Expense Tracker CLI Application
Build a command-line expense tracker application in Python that allows users to add, view, and categorize their expenses. The program should support adding multiple expenses, filtering expenses by category, and displaying the total amount spent.
Challenge prompt
Create a Python program that functions as an expense tracker. Your application should allow users to: 1. Add expenses including amount, description, and category. 2. View all added expenses in a readable format. 3. Filter expenses by category to see only expenses related to that category. 4. Calculate and show the total amount spent across all expenses. Your program should continue to accept commands until the user decides to exit. Implement this functionality using functions and a list to store expense data as dictionaries.
Guidance
- • Create a function to add an expense capturing amount, description, and category.
- • Use a list to store all expense entries as dictionaries.
- • Create functions to filter expenses by category and calculate the total amount spent.
- • Implement a simple user interface loop to accept user commands until they choose to exit.
Hints
- • Use dictionaries with keys like 'amount', 'description', and 'category' to represent each expense.
- • Consider using a while loop with a menu system to prompt the user for actions.
- • When filtering expenses, loop through the stored list and only print entries matching the category.
Starter code
def add_expense(expenses):
# Implement adding an expense
pass
def view_expenses(expenses):
# Implement viewing all expenses
pass
def filter_expenses(expenses, category):
# Implement filtering expenses by category
pass
def total_expenses(expenses):
# Implement calculation of total amount spent
pass
def main():
expenses = []
while True:
print("\n1. Add expense\n2. View expenses\n3. Filter expenses by category\n4. Show total spent\n5. Exit")
choice = input("Choose an option: ")
if choice == '1':
add_expense(expenses)
elif choice == '2':
view_expenses(expenses)
elif choice == '3':
category = input("Enter category to filter: ")
filter_expenses(expenses, category)
elif choice == '4':
total = total_expenses(expenses)
print(f"Total spent: ${total:.2f}")
elif choice == '5':
print("Goodbye!")
break
else:
print("Invalid choice, please try again.")
if __name__ == '__main__':
main()Expected output
Example run (user inputs are shown after prompts): 1. Add expense 2. View expenses 3. Filter expenses by category 4. Show total spent 5. Exit Choose an option: 1 Enter amount: 25.5 Enter description: Lunch Enter category: Food 1. Add expense 2. View expenses 3. Filter expenses by category 4. Show total spent 5. Exit Choose an option: 2 Expenses: - $25.50 | Lunch | Food 1. Add expense 2. View expenses 3. Filter expenses by category 4. Show total spent 5. Exit Choose an option: 4 Total spent: $25.50 1. Add expense 2. View expenses 3. Filter expenses by category 4. Show total spent 5. Exit Choose an option: 5 Goodbye!
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.