# 12 Stocking Out


#1



Can you please look at my code?



Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7


def compute_bill(food):
    total = 0
    for item in food:
        if item in stock > 0:
            total = total + prices[item]
            stock[item] = stock[item] -1
    return total
    print compute_bill(food)


#12 Stocking Out
#2

this:

if item in stock > 0:

won't work. you can get access how many items are left in stock through: stock[item], hope that helps


#3

I don't know if I am better worse
def compute_bill(food):
total = 0
for item in food:
if item in stock[item] > 0:
total = total + prices[item]
stock[item] = stock[item] -1
return total
print compute_bill(food)
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: argument of type 'int' is not iterable


#4

stock[item] gives you the number of items left in the stock, so all you have to do is check if it is greater then 0

in is used to check if a item is in a list or dictionary, not if the value is higher then zero


#5

thank you for your help


#6

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