Stocking out HELP!


#1

I have tried everything but still can't figure this out when I click submit this error code pops up.

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7: my code is as follows

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

don't know what I'm doing wrong::confused: link to problem is https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096#


#2

The problem is in your 'if' statement. The way you have it written, it's looking at your stock item, making sure it's greater than 0, and then subtracting 1 from that item, instead of from the dictionary in which that item is listed. That's why your total is wrong! Here's a potential solution:

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

Hope it helps!


#3

Thank You it did help


#4

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