Back to Freecodecamp

Step 38

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

latest2.2 KB
Original Source

--description--

You are going to use conditional statements to check the user's choice. If the choice is '1', it means the user wants to add an expense.

Still in the while loop, under the choice variable, write an if statement to check if choice equals the string '1'. If it's true, it will be the starting point for adding a new expense.

Inside the if statement body, declare a variable amount and assign it an empty input() call.

--hints--

You should create an if statement that checks if choice is equal to '1' in your while loop.

js
({ test: () => assert(runPython(`
cond = _Node(_code).find_function("main").find_whiles()[0].find_bodies()[0].find_ifs()[0].find_conditions()[0]
cond.is_equivalent("choice == '1'") or cond.is_equivalent("'1' == choice")
`)) })

You should declare a variable named amount in your if statement body.

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

You should assign an empty input() call to your amount variable.

js
({ test: () => assert(runPython(`_Node(_code).find_function("main").find_whiles()[0].find_bodies()[0].find_ifs()[0].find_bodies()[0].find_variable("amount").is_equivalent("amount = input()")
`)) })

--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)
    
--fcc-editable-region--
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--