Function's argument should be a list or a dict?


#1



I am puzzled...I get this error message "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not str"

In the instructions it the function should have the argument "food" list, where as for the function to take both the type of food, and the quantity, it seems it should be a dictionary.

I do understand in the equation, total = total + food[key]*prices[key], since "key" is a string, hence the error. My problem is I can't figure out where the quantity of food items we want to buy, and how they get into the function's argument if it is only a list - is the list just numbers in the order of the "stock" dictionary items?


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096


shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:
            total = total + food[key]*prices[key]
            stock[key] -= stock[key]
    return total


#2

['apple', 'apple', 'apple']

#3

OK. I got it. But, my code is still not calculating correctly...help please.

def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= stock[item]
return total


#4

Your bug is fairly obvious, you need to get a better understanding of what you wrote, perhaps by just reading it carefully and comparing to what you want to happen.
You can also add prints to check if it's doing what you mean for it to do as it runs.
Start by considering what it's doing differently from what it is supposed to and what could be causing that.