11. Making a Purchase



compute_bill(['banana', 'apple', 'orange', 'pear']) returned 8 instead of 10.5

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 item in food:
        total += prices[key]
    return total


Hi this line

total += prices[key]

you never define key but you define item so change it.


I don't understand. Can you please explain?


where you put key you should put item here

total += prices[key]


Thanks, it worked, but I still don't get how. What was wrong in the initial code?


This line

for item in food:

you loop through each item inside food (food wihch is the parameterof teh function) then on this line

total += prices[key]

you wanted to acces the price of each product with prices[item] and make the sum of all the prices
so since you didn't use key in the for loop bu item it's right to use item


Thanks, will keep that in mind.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.