pythonintermediate15 minutes

Build a Personal Expense Tracker Mini Project

Create a Python program that allows users to add, view, and analyze their personal expenses over time, helping them track spending habits.

Challenge prompt

Build a Python function named `expense_tracker` that manages personal expenses. The function should allow users to add new expenses (including date, category, and amount), view all expenses, and get a summary of total spending by category. The program should store expenses in a list of dictionaries, where each dictionary represents one expense. Provide options to add an expense, view all expenses, and show total expenses grouped by category. Your function should continue prompting the user until they choose to exit.

Guidance

  • Use a list of dictionaries to store expenses, with keys such as 'date', 'category', and 'amount'.
  • Implement a simple text-based menu to allow users to select actions like adding expenses, viewing expenses, and summarizing spending.
  • For summarizing, iterate over the expenses and accumulate totals per category.

Hints

  • Consider using a while loop to keep the menu running until the user opts to exit.
  • To group and sum amounts by category, you can use a dictionary with categories as keys and sums as values.
  • Remember to convert amount input to a float for proper mathematical operations.

Starter code

def expense_tracker():
    expenses = []
    while True:
        print('1. Add Expense')
        print('2. View Expenses')
        print('3. Summary by Category')
        print('4. Exit')
        choice = input('Choose an option: ')
        if choice == '1':
            # Add your code to add an expense here
            pass
        elif choice == '2':
            # Add your code to display all expenses here
            pass
        elif choice == '3':
            # Add your code to summarize expenses here
            pass
        elif choice == '4':
            break
        else:
            print('Invalid option, please try again.')

Expected output

After adding some expenses and choosing 'View Expenses', the output should list all inputs, e.g., date, category, and amount. Choosing 'Summary by Category' should show total amounts spent in each category, e.g., Food: 150.00, Transport: 75.50.

Core concepts

lists and dictionariesloop and conditionalsdata aggregationuser input handling

Challenge a Friend

Send this duel to someone else and see if they can solve it.