Stocking out 'A Day at the Supermarket'


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

Who knows why the code above needs:
stock[item] = stock[item] - 1 ?

I spend a lot of time trying to figure it out,
First, program did not let me pass without that code, but after I renewed the page
it let me pass without 'stock[item] = stock[item] - 1'
without 'stock[item] = stock[item] - 1' it still computed right answer but gave me the random error which is so far from what I called (I called: print compute_bill(shopping_list))
it's gave me 5.5 but did not let me pass.


It is telling the interpreter to subtract one from the stock dictionary. :slight_smile:


I know that, but the code works right even without it


You said it works right but gives you an error, meaning it would not pass even on an actual python interpreter


Check what they asked you in order to code: "Ignore whether or not the item you're billing for is in stock."


Sorry, forget what I've written. I was at the previous exercise.


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