Stocking Out - HELP


#1



What is wrong???


Replace this line with your code. 
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:
        if stock[item] > 0:
            total += prices[item]
            stock = stock[item] - 1
    return total


#2

here:

stock = stock[item] - 1

you don't reduce the stock correctly, what is happening here is that you overwrite your stock dictionary with an integer

update the stock value of a specific item, don't overwrite your whole dictionary with this value


#3

what do you mean update the stock value of a specific item?? Sorry I'm still confused confused


#4

lets say customer buys a banana then we get:

stock = stock["banana"] - 1

then we get:

stock = 6 - 1

so now stock is 5, but this mean we overwrote our dictionary (containing product and there quantity) with an integer, ouch, now when a next customer wants to buy something, we have a problem. Our stock no longer contains products and there quantity, only a integer)

you should have reduced the quantity of banana (item), not overwrite our entire stock with only how many banana's are left


#5

So what should it be instead of:
stock = stock[item] - 1
???


#6

to update a value associated with a key, there is the following general syntax:

dictionaryName[keyName] = "new value"

#7

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