12/13 got some funny results


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>
My code was marked as correct. Not sure why, and would like to figure out how to actually get it to be correct.

<What do you expect to happen instead?>
I expected it to print out one item in the shopping list at a time, not the entire list. I know it goes on and on because the function has it de-increment until it reaches 0, just not sure why it prints the entire shopping list every time, is it because the exercise is inserting the entire shopping list as a var for food?



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 += prices[item]
      stock[item] -= 1
    elif stock[item] == 0:
      print "No more %s in stock" % food
  return total

output:

No more ['apple', 'banana', 'pear'] in stock
No more ['apple', 'pear', 'pear', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'pear', 'pear', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['pear', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['pear', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['pear', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['pear', 'pear', 'pear', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'pear', 'pear', 'pear', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'pear', 'pear', 'pear', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'pear', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'orange', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock
No more ['apple', 'orange', 'banana', 'banana', 'banana', 'banana', 'banana'] in stock

#2

when one product is not in stock, you print out all the items customer wants to buy (food), why not print item that is no longer in stock?

the result you see, are all the lists tested by the exercise (the exercise calls the function multiply times, to verify correct data is returned)


#3

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