11. Making a Purchase


#1



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


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


#2

Hi this line

total += prices[key]

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


#3

I don't understand. Can you please explain?


#4

where you put key you should put item here

total += prices[key]

#5

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


#6

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


#7

Thanks, will keep that in mind.


#8

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