Why does it ask me not to use compute_bill?

Question

Why does it ask me not to use compute_bill?

Answer

If you think you’ve coded your function properly and are receiving an error message along the lines of stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock!, this is because in this step we aren’t using the function after writing it, we’re just writing the function.
Try deleting where you used the function and then Run your code again.
It’s great to want to test your code! However, Codecademy runs tests to check your code that depend on the values being the original values, and running the function in your code in this step changes those values. You’ll be able to use it in the very next step!

4 Likes

Could you say me where is my problem

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 not stock[item] <= 0:
      total += prices[item]
      
    else:
      print (("Stock has no %s") % item)
  stock[item] -= 1
  return total

print (compute_bill(shopping_list))

i would be possible to buy 9 bananas:

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 not stock[item] <= 0:
      total += prices[item]
      
    else:
      print (("Stock has no %s") % item)
  stock[item] -= 1
  return total

print (compute_bill(['banana'] * 9))

while there are only 6 bananas in stock

2 Likes

Guys! Where is the key to solve the exercise?

i told you, if i attempt to buy 9 bananas, i can. Which shouldn’t be possible, because there are only 6 bananas in stock.

Hi,

I’m not sure if this solves your problem, but try indent the stock[item] -= 1 more.
It seems to me that currently it sits on the same level as the ‘for loop’ (i.e under the main function level) but it should be under the ‘for loop’ since we need to do that subtraction in each loop.

I hope this helps

cheers,
D.

Close, (s)he should only decrease stock when the item isn’t in stock, so it should be nested inside the if clause

1 Like

I guess you wanted to write …the item is in stock…

anyway, it is strange because that’s what I thought at first but putting that line inside the if clause caused an indent error. So I went ahead and tried other indents. And strangely only the above mentioned (by me) worked.

today I went back and tried your suggestion again, and it worked now. pretty odd.
maybe it has something to do with tab vs space indentation?

the indent settings within the codecademy editor are not ideal.

A post was split to a new topic: A Day at the Supermarket: Why is this wrong?

You need to put stock[item] -= 1 in the if not statement you have it in the else statement.

I’ve been experiencing many IndentationError messages (in this as well as previous exercises) despite my code being identical to the solution and Codecademy wouldn’t allow me to move on unless I went for the Solution. I even tried deleting the indents of the error Line and reintroducing them in a consistent manner, but it still wouldn’t accept my code. Is there another solution? Do I need to delete all indents of every line and consistently reintroduce them line by line for my code to be recognized?

My code:

Solution code:

codecademy’s editor sucks when it comes to indent. I would re-indent the code in your own local editor, if you haven’t set this up, you could use sites like repl.it

Ok, thanks for the suggestion, I’ll try that next time.

Any idea what I’m doing wrong?

The dictionary value is not updated when you just minus 1 from the value, you then need to also set the dictionary value to the value minus 1.

stock[item] = stock[item] - 1

4 Likes

This was exactly the problem. Thank you!

The problem for me was the stock[item] = -1 did not work. I needed to make it stock[item -= 1

why am I getting errors with this code?

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

That is literally what this FAQ answers: