Learn Lambda Functions by Building an Expense Tracker - Step 36

Tell us what’s happening:

Describe your issue in detail here.

Hi team. I can’t go beyond this step. I can’t find what is wrong with my code.

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

        if choice == '1':
            amount = float(input('Enter amount: '))
            category = input('Enter category: ')
            add_expense(expenses, amount, category)

/* User Editable Region */

        elif choice == '2':
            print('\nAll Expenses:')
            print_expenses(expenses)
        elif choice == '3':
            print('\nTotal Expenses:',\nTotal Expenses:)
        

/* User Editable Region */

Your browser information:

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

Challenge Information:

Learn Lambda Functions by Building an Expense Tracker - Step 36
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-lambda-functions-by-building-an-expense-tracker/step-36`Preformatted text`

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

Here is my code:

elif choice == '2':
            print('\nAll Expenses:')
            print_expenses(expenses)
        elif choice == '3':
            print('\nTotal Expenses:', total_expenses(expenses))

pass the string \nTotal Expenses: as the first argument

Carefully check this string

1 Like

Hello,

It looks like there are a couple of issues in your code. I’ve provided a corrected version

<REDACTED BY MOD>

Here are the changes made by me:

  1. Removed the /* User Editable Region */ comments as they are not required in Python.
  2. Added the calculation for total expenses in the ‘3’ option inside main().
  3. Added an option ‘4’ to filter expenses by category and display the filtered expenses using the filter_expenses_by_category and print_expenses functions.
  4. Added an if __name__ == "__main__": block to ensure that the main() function is only called when the script is run directly, not when it’s imported as a module.

Regards
Jack Smith

1 Like

hi @machbrown2

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

this are steps done going forward in the project, when you give help I suggest you check what step the camper is doing so you know what kind of help they need

1 Like

I came to this topic because I had exactly written the code I was told for this step but it was not passing. I refreshed the page to see if I got the chance to type everything again. I retyped elif choice == '3': option. Reviewed the code on option 3. But to no luck.

When I was about to write my question here, I had a last look at my code and found out that within it I had written — by mistake — a letter (a) at the beginning of the line elif choice == '2':, like this:

a         elif choice == '2':
                      print('\nAll Expenses:')
                      print_expenses(expenses)
          elif choice == '3':
                     print('\nTotal Expenses:', \nTotal Expenses:)

After deleting it, the code passed. Of course :sweat_smile:.

This is only a reminder to really be careful with what you type.
And when you are stuck, just step back and start over from the beginning of the exercise. Go line by line. Erase the new code (all of it), if you get too much stuck. Retype everything carefully.