11. Making a purchase


A day at the Supermarket - Making a purchase.

my code:

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

#price and stock omitted

# Write your code below!
def compute_bill(food):
    total = 0
    for food in shopping_list:
        total = total + prices[food]
    return total

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

I don't understand why I can't call the values within shopping_list as variables "food".
I know now after a search in the FAQ that the following is correct:

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

Why can't I use "food" as the variable instead of "item"?
Is it because I called "shopping_list" as the reference list?

Appreciate your help, thanks!


Are we not iterating over a list, food, with each, item, being priced out?

The mechanics are fairly simple. If we define a parameter as 'A' and then within the same function define 'A' some other way, we've lost the argument.


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