### Some help please, A day at the Supermarket compute_bill ERROR - Not sure why


#1

Can someone help me understand why is not working?Thanks in advance

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


A day at the Supermarket compute_bill(['apple´)]returned 10.5 instead of 2

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

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

def compute_bill(food):
    total=0
    for i in prices:
         print i
         print prices[i]
         total=prices[i]+total
         print total
    return total
#compute_bill(['apple'])
compute_bill(shopping_list)

#2

Here:

def compute_bill(food):
    total=0
    for i in prices:
         print i
         print prices[i]
         total=prices[i]+total
         print total
    return total

you are just summing up prices of all the products in the prices dictionary.

What you want to do is to sum prices of products that are inside the food list:

def compute_bill(food):
    total=0
    for i in food:
         print i
         print prices[i]
         total=prices[i]+total
         print total
    return total
#compute_bill(['apple'])
compute_bill(shopping_list)

#3

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