Amending a dictionary in a lop


#1


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#

If the item is in stock and after you add the price to the total, subtract one from the item's stock count.


I don't know how to update the value of the dictionary item.
File "python", line 23
stock.update({item:stock[item] =- 1})
^
SyntaxError: invalid syntax


I would like to amend the dictionary


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.update({item:stock[item] =- 1})
    return total

#2

item contains the item (apple, pear, and so on) of which you want to update the stock, so can simply subtract one from the item in a stock?

just like you update total here:

total += prices[item]

you can update stock:

stock[item] -= # update stock

you are over complicating


#3

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