# Stocking Out - Wrong Total

#1

"Oops, try again. calling compute_bill with a list containing 8 bananas resulted in 32 instead of the correct 24"
or
"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
x=0
for item in food:
currentItem = food[x]
if stock[currentItem] > 0:
total = total + prices[currentItem]
#stock[currentItem] -= 1
x += 1

#2

the problem is here:

``currentItem = food[x]``

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

#3

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

#4

I iterate x at the end of my for loop

#5

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

#6

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

#7

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

#8

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

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

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

#9

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

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

#10

so the indent will show

#11

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?

#12

`-=` 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

#13

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

#14

what do you mean?

#15

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