12. Stocking out


#1

My code returns the error "calling compute_bill with a list containing 2 pears and 9 bananas resulted in 42 instead of the correct 30"

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 items in food:
        if stock[items] > 0:
            total = total + prices[items]
    return total

#2

if you charge a user for a item:

total = total + prices[items]

you should also reduce the stock by 1:

total = total + prices[items]
// decrease stock here

#3

new to programming... not quite sure how to call the stock[key] and then subtract from it...

total += prices[key]
stock[key -1]

doesnt work and neither does

stock[key] - 1


#4

got it:
stock[key] = stock[key] - 1


#5

Very good! You solved it yourself with just a little push in the right direction. Well done :slight_smile: