Help for this lesson for checking stock


#1




Logically this makes sense in my head... I mean if the stock is greater than 0 then run this line of code... yes. stock[calling item] item looks in the food list am I correct? total += prices[item] for the prices in the food list. correct in the previous lesson. Then I added the line stock[item] - 1. This makes sense in my mind, then I begin to wonder... Am I not getting this whole dictionary + functions things? Can anyone explain if I'm getting it wrong. And return total to close to close the function :slight_smile:


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


#2

this:

stock[item] - 1

won't reduce the stock, look:

i = 10
print i # 10
print i -5 # 5
print i # 10
i = i - 5
print i # 5

hope that helps

there is a reason you use += increasing total, and not just +


#3

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