Stocking out


Write your code below!

 stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15

 prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3

def compute_bill(food):
total = 0
for key in food:
     if stock[key] > 0 and food[key]<= stock[key]:
          total += prices[key]* food[key]
          stock[key] = stock[key]- food[key]
          return total
print compute_bill(['pear','banana','apple'])
print compute_bill({'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0})

why is it not working?


Your if is kinda strange :smiley:
Your stock just needs to be greater than zero no need for and, after that you need to add it to your total variable, don’t need to multiply it with your food list
When you done that subtract your stock


why if is strange? food[key]must be smaller or equal to stock[key] .for greater food[key] there will be no stock.and we also need to multiply prices[key] with food[key] that how we will obtain total bill .
I don’t get your answer


You just need to iterate with you for loop over food to get all the values, all that values are stored in key, you don’t use food anymore in function

your stock just needs to be more than 0, if you have 6 bananas, you add price to your total, and remove that 1 banana out of your stock

try to thing about it like in real life, you can’t sell something if you don’t have it in your stock, if you sell of your 6 bananas, you have five left in your stock