Code count right, but error tell me the wrong count


#1

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(shopping_list):
    total = 0
    for item in shopping_list:
        if (stock[item]>0):
            total = total + prices[item]
            #stock[item] = stock[item] - 1
    return total
    
print compute_bill(shopping_list)

The error tell me this:

Oops, try again. calling compute_bill with a list containing 1 apple and 8 bananas resulted in 32 instead of the correct 24

why does it tell me the correct answer is 24? 8 bananas for 4 price each should resulted 32, not 24

where do I get myself wrong??


#2

Of course, 8 bananas cost 32, BUT, you don't have 8 bananas in your stock :wink:


#3

i have exactly the same problem and still don't understand why )

we do not count price of goods in stock in this exercise . we just need to add price of single fruit listed in shopping_list and decrease number in stock. why it verifies cost of 8 bananas??? and why 8 but not 10 for example? )

moreover, if I remove # from the next line i will get another error:
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}

looks like it expects that number in stock must remain unchanged. it doesn't comply with task #2: If the item is in stock and after you add the price to the total, subtract one from the item's stock count.


#4

Your function should modify the stock, it says so in the instructions.

But the stock should be unchanged when execution reaches the bottom of your code file, that is what the instructions mean with "do not call your function"

It tests with 8 bananas because that is 2 more than there are in stock which means that to pass you need to have added the functionality described in this exercise, in other words it tests if you implemented what is described in the instructions.

See it as an exercise in implementing specifications precisely, that is very important.


#5

Hey, it works as you paste (withour #), just delete last line -> print compute_bill(shopping_list)
:slight_smile:
The point is in error descrpition - you just cant call this function becose it's change the values of stock, and those values has to remain unchanged to pass the lesson. It's a little confusing but that's how code supervision here works.