Stocking Out - Wrong Total


"Oops, try again. calling compute_bill with a list containing 8 bananas resulted in 32 instead of the correct 24"
"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}"

So I have 2 errors depending if I uncomment/comment my stock decrement line

Commenting out:
So I understand what's wrong, the calculation is wrong because the stock count is only checking if it's greater than 0. If I'm looking for 7 bananas and there are only 6, it's calculating as if I'm buying 7. However, I don't see where the amount of user items are being passed.

If I don't comment out:
Then it says that I'm changing the stock amount, however, one of the instructions says to decrement the stock explicitly:
"If the item is in stock and after you add the price to the total, subtract one from the item's stock count."

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


the problem is here:

currentItem = food[x]

x is always zero? so currentItem is always the first item in the list?


There's a suggestion in the error message about what to do differently


I iterate x at the end of my for loop


The instruction in the lesson is to decrement stock as I indicated in my original post, however, the error message says to not change stock values. That's why I also tried commenting out that line that changes the stock value


Omitting the call to the function also has the effect of not changing the stock, while still keeping the function true to the specification


why would you use a for loop and increase x? that is a bit double


Agreed, I thought it was the manner that I was traversing the tuple so I tried to be more explicit:

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

here is edited code - still though this does not change the error


Maybe it's the indentation. I guess stock substraction should be on the same line with if statement.

My code worked that way:

def compute_bill(food):

total = 0

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

P.S. I try to avoid += and -= because they always give errors.


please post your full code using markup:

so the indent will show


it doesn't want the stock to be altered

but in the instructions it says to decrement the stock

with regard to += or -= I thought that was pythonic?


-= and += are perfectly valid, and you should use them.

no, the exercise doesn't want you to call the function, this will alter the stock before the exercise can validate your code


That's the other part of the question.

How are the quantities being passed? Because if it is not decremented that the proper totals are not being accurately reflected


what do you mean?


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