11. making a purchase


Why is the encoder giving me this error:

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

When I write this:

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

Wouldn't that be exactly what should be returned when you get to the apples on the list?

11.making a purchase error

you should loop over the argument passed into the function:

for x in food:

the exercise runs a few cases, to see your code is correct. Currently, use the the shopping_list from outside the function, which means if the exercise calls the function with a different list, that list is never used


I used:

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

print compute_bill(shopping_list)

I think what happens is that shopping_list is recognized as food in my function compute_bill.
Then the function draw the values of my variable food from my prices list to give my total.

Am I right?


Yep, you are absolutely right. the variable food will hold your shopping lists, since you pass it as argument to the function. And then you get the price from the total dictionary, and voila.