11.making a purchase error


#1



Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable


Replace this line with your code. 
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 is wrong?


#2

total += prices(food)

food will be a list. that is why you have the for loop:

for key in food:

now key will hold the actual items. Also, to access a value from a dictionary, you need to use square brackets:

prices[key]

#3

Thank you I finally got it.


#4

def compute_bill(food):
total = 0
for item in food:
total = prices[item] + total
return total# Write your code below!

Why am I still get wrong in this one?

Oops, try again.
compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

#5

your return is wrongly indented. The moment a return keyword is reached, the function ends (including the loop inside the function, obviously. Re-indent your return, so it is outside the for loop, so the whole for loop will run


#6

Oh got it! Thanks!!!


#7

A post was split to a new topic: 11. making a purchase