Stocking Out - exercise 12


My code below generates the message "Oops, try again.
stock doesn't look quite right! Make sure to not call compute_bill
since it changes the stock! It should contain: {'orange': 32, 'pear':
15, 'banana': 6, 'apple': 0}"

My code DOES decrement the stock, as it is supposed to, but the error message implies that it is NOT supposed to. Does anyone know what is wrong with what I have?

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

print compute_bill(shopping_list)


remove the function call:

print compute_bill(shopping_list)

it changes the values in the dictionary


Thank You!! That worked! But I really don't know why you wouldn't want to change the values in the dictionary, The exorcise DID say to remove available items from the inventory and that would be the values in the dictionary, right?


yes, inside the function the stock should be reduced

However, calling the function will actually change the stock before the checking script is called, causing incorrect answers


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