Back to Freecodecamp

Step 42

curriculum/challenges/english/blocks/learn-lambda-functions-by-building-an-expense-tracker/6582575b8089f85b8b92d7c8.md

latest2.4 KB
Original Source

--description--

Once you have the expense details, you need to call the add_expense function to add the new expense to the expenses list.

After getting the amount and category using input(), call the add_expense function, passing three arguments: expenses, amount and category.

  • expenses is the empty list created in the main function earlier in this project.
  • amount is the amount of the expense.
  • category is the category of the expense.

--hints--

You should call the add_expense() function within your if statement.

js
({ test: () => assert(runPython(`len(_Node(_code).find_function("main").find_whiles()[0].find_bodies()[0].find_ifs()[0].find_bodies()[0].find_calls("add_expense")) == 1`)) })

You should pass three arguments to your add_expense() call: expenses, amount, and category. The order matters.

js
({ test: () => assert(runPython(`_Node(_code).find_function("main").find_whiles()[0].find_bodies()[0].find_ifs()[0].find_bodies()[0].has_call("add_expense(expenses, amount, category)")`)) })

Your add_expense() call should come after the assignment of amount and category.

js
({ test: () => assert(runPython(`_Node(_code).find_function("main").find_whiles()[0].find_bodies()[0].find_ifs()[0].find_bodies()[0].is_equivalent("amount = float(input('Enter amount: '))\\ncategory = input('Enter category: ')\\nadd_expense(expenses, amount, category)")`)) })

--seed--

--seed-contents--

py
def add_expense(expenses, amount, category):
    expenses.append({'amount': amount, 'category': category})
    
def print_expenses(expenses):
    for expense in expenses:
        print(f'Amount: {expense["amount"]}, Category: {expense["category"]}')
    
def total_expenses(expenses):
    return sum(map(lambda expense: expense['amount'], expenses))
    
def filter_expenses_by_category(expenses, category):
    return filter(lambda expense: expense['category'] == category, expenses)
    

def main():
    expenses = []
    while True:
        print('\nExpense Tracker')
        print('1. Add an expense')
        print('2. List all expenses')
        print('3. Show total expenses')
        print('4. Filter expenses by category')
        print('5. Exit')
        
        choice = input('Enter your choice: ')
--fcc-editable-region--
        if choice == '1':
            amount = float(input('Enter amount: '))
            category = input('Enter category: ')
            
--fcc-editable-region--