Explaination for "Making a Purchase"


#1

So given 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
}

This works:

def compute_bill(food):

total = 0

for item in food:

    total += prices[item]

print total

return total

Why does this not work:

def compute_bill(food):

total = 0

for food in shopping_list:

    total += prices[food]

print total

return total


#2

Variable conflict. Your code has a parameter, food, which is a shopping_list, but then the loop overwrites that variable by making it the iterator in for..in

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

    #print total
    return total

print compute_bill(shopping_list)

#3

... code works but my question is where did they get prices from? I mean where do they come up with that from? Stupid question i'm sure!!!


#4

From the prices dictionary.

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}