Stocking


#1

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

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

    
value = compute_bill(stock)
print value

Can anyone please tell whats wrong in the above code ?? For I am not getting the correct answer


#2

the exercise doesn't want you to call your function for some weird reason i haven't figured out. remove these two (the last two) lines:

value = compute_bill(stock)
print value

#3

Hey Even after removing the last 2 lines I still get the error. By the way I have been asked to print the total value for which I used the above two lines . I have done some changes and I am still getting the error

shopping_list = ["banana", "apple", "orange"]
prices = {
    "banana" : 1,
    "apple"  : 2,
    "orange" : 1,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}

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

#5

why did you change the prices in the dictionary? That is going to cause trouble. If i look at the error message:
stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock!

So i remove the function call:

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

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

and it works fine