Stocking


#1

Can anyone please tell whats wrong in this code even though my output is correct .

prices = {
"banana" : 3,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

def compute_bills(food):
total = 0
for key in food:
if stock[key] > 0 :
total += prices[key]* stock[key]
stock[key] -= 1

return total
shopping_list = { 'apple' , 'banana' , 'orange'}
print compute_bills(shopping_list)


#2

can you write the output error ??


#3

error in this line :
total += prices[key] * stock[key]
Instructions :
While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
also name your function compute_bill not compute_bills


#4

Hey the error is

Oops, try again. You shouldn't need to write out any of the food products.


#5

i did use your code but didn't get the same error you get !!
did you do what i say in this reply !!


#6

Here is the code I used which worked without defining a new function:

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

total = 0
for key in prices:
total += prices[key] * stock[key]
print total