"making a purchase" gives problems with the "Apple" item


#1



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


Error:
Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2


I expected it work because the calculation seems right.


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


#2

lets see what happens when customer wants to buy an apple:

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

print compute_bill(['apple'])

that is kind of unfair, i get charged for apple, orange and banana while only buying an apple. Lets see what happens when a second customer buys a banana:

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

# first customer buying an apple
print compute_bill(['apple']) 
# second customer buying a banana
print compute_bill(['banana'])

again, same problem. Both customuters gets charged more then for the products they are buying

Why did you give your function a parameter (food) when you are not going to use it?


#3

It worked!
Thanks, I used:

Write your code below!

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

I was confused because they never mentioned that food was equivalent to "shopping_list".


#4

that its because its not, shopping_list could be argument for function call

its in the instructions:

For each item in the food list

that you should loop over food


#6

maybe you can change shopping_list to food and at the last add some word like "compute_bill(shopping_list)"


#7