11 making an purchase


#1
`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:
        total += prices[food]
    return total`

What's wrong here?


#2

key in food or food in prices ??


#3

I don't know, I don't get the dictionary part of python. :confused:
This is the error it is giving me, "compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable"


#4

your code here is good , the error is that you loop in food ( compute bill argument ) by key
so to compute total you have to get prices of food , you can't select prices of all food so
you select one food ( key ) to get that your code will be prices[key] not prices[food]


#5

Works, thanks :slight_smile: