12.Stocking Out(Need explanation)


#1

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

Why stock[item]-=1?

(This is correct code if anyone stuck)


#2

Hi @l1palas,

Hint: Whenever you purchase an item from a shop, then the stock of the shop for that item decreases by 1.


#3

k i got it now, thx

..


#4

(Code from original post by @l1palas)

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

It still gives an error. It says item is not defined


#6

check your indentation after
if stock[item] > 0:


#7

Wouldn't the correct for loop be

for item in food

You are using n, but then not using it in your calculations