Making a purchase


#1

I have a question:

In my function compute_bill, when I am iterating over "food",
doesn't "item" mean each key-value pair in the dict "food"?

and if so, "item" would mean, say, (banana": 4)
then how does "prices[item]" work?

Please clear my confusion!

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 food:
        total += prices[item]
    return total
print compute_bill(prices)

Making a Purchase
#2

actually, compute_bill is a function which calculates the cost of the user purchases, for example:

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

so, we supply a list of products the customer want to buy as argument when calling the function. Then a different customer might want to buy different things:

def compute_bill(food):
    total = 0
    for item in food:
        total += prices[item]
    return total
# customer 1
print compute_bill(shopping_list)
# customer 2
print compute_bill(["apple", "pear"])

and so on. (customer 3, customer 4 etc)

then you had a question about this line:

total += prices[item]

i hope you now realize food is a list, only containing products, but we need to present our customer with the total cost of there purchases, so of each item we need to get the price of item from prices dictionary


#3

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