Learn Lambda Functions by Building an Expense Tracker - Step 21

Tell us what’s happening:

Step 21
Within the filter_expenses_by_category function, replace pass with a lambda function. Use expense as the parameter and return the comparison between the value of the ‘category’ key of the expense dictionary and category using the equality operator.

This is my answer:
def filter_expenses_by_category(expenses, category):
return filter_expenses_by_category(lambda expense: expense[‘category’] == category, expenses)

But, it saying: Sorry, your code does not pass. Keep trying

Your code so far

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))
    

# User Editable Region

def filter_expenses_by_category(expenses, category):
    return filter_expenses_by_category(lambda expense: expense['category'] == category, expenses)

# User Editable Region


expenses = []

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Lambda Functions by Building an Expense Tracker - Step 21

Hi, here you are trying to call your function filter_expenses_by_category within itself. That is not what the step is asking you to do. Instead, just replace the pass word with your lambda function, which seems good to me :slight_smile: Happy coding!

The instruction also state that: ‘return the comparison between the value of the ‘category’ key of the expense dictionary and category using the equality operator.’ which is what i was trying to do
And also i did what you said and it didnt work

Please share your updated code.

def filter_expenses_by_category(expenses, category):
lambda expense: expense[‘category’] == category
return filter_expenses_by_category(expense[‘category’] == category, expenses)
I just tried this now and it worked
thank you so much

Please, be aware that this line was not required and it would raise an error if the function would be called.

Yup this step took me forever… it says it wants you to return the value but you do NOT return anything…

It wants this

def filter_expenses_by_category(expenses, category):
lambda expense: expense[‘category’] == category

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.