Stocking Out


#1



Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: local variable 'stock' referenced before assignment

i think i am subtracting the item from the stock once its price is added. Dont know where is it 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 items in food:
        if stock[items] > 0:
            total += prices[items]
            stock -= stock[items]
    return total


#2

hint: you put 1 item into the cart, so you'll want to subtract 1 from somewhere


#4

that should be subtracted from the stock. correct?


#5

stock is the dictionary, by using the key (items) you can get the value (amount left in stock), this you need to reduce


#6

aahh its really not working! could you please help me?


#7

can i see an updated version of your code?


#8

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 += prices[items]
            stock = food[items] - 1
    return total

i think i am going in a very different direction.


#9

okay, this line:

stock = food[items] - 1

you are updating/re-assign stock, agree? but what is stock? it is a dictionary.

Is that really what you want to update? items contains the actual product, so if you where to do: stock[items] you would get the number of items left in stock

Doesn't it make more sense to update the items left in stock? so stock[items] should be at the left hand side of the equal sign


#10

got it now mate. thanks much


#11