Problem in lesson 12


#1

Hello

I don't know how i can check is there is enough stock for the demand.

Is there anyone that can help me?

Here is my 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 = total + prices[item]
            stock[item] - 1
    return total

#2

look at the following code:

i = 10
print i # 10
print i + 5 # 15
print i # 10
i = i + 5
print i # 15

now look at your stock reduction:

stock[item] - 1

the stock will not reduce, you really need to update the variable. As the other code with i shows, you can temporary add (or subtract), this is different from updating variables


#3

Thank you for the fast reply!


#5

Or you can just change stock[item] - 1 to stock[item] = stock[item] - 1 or to stock[item] -= 1


#6