12.Stocking out help!


#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

<In what way does your code behave incorrectly? Include ALL error messages.>
Output console shows:
banana : 6 total = 4
orange : 32 total = 5.5

<What do you expect to happen instead?>
It doesnot loop over the whole stock until it ends. It only loops once.
I added another loop on the top
def compute_bill(food):
total=0
for num in stock:
for num in food:
if(stock[num] > 0):
total=total+prices[num]
print “%s : %s total = %s” %(num, stock[num], total)
stock[num]-=1
return total

compute_bill(shopping_list)

then it loops 4 times which i understand is for number of keys in stock which is 4. and output is
banana : 6 total = 4
orange : 32 total = 5.5
banana : 5 total = 9.5
orange : 31 total = 11.0
banana : 4 total = 15.0
orange : 30 total = 16.5
banana : 3 total = 20.5
orange : 29 total = 22.0

I think i didnt understand the assignment as it gives a message at the bottom
"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}

"


def compute_bill(food):
  total=0
  for num in food:
    if(stock[num] > 0):
        total=total+prices[num]
        print "%s 	: %s	total	= %s" %(num, stock[num], total)
        stock[num]-=1
  return total

compute_bill(shopping_list)




Help me with 12. Stocking out
#2

remove the function call for a second:

compute_bill(shopping_list)

otherwise the stock gets manipulated before validation, and you get the error message:

Make sure to not call compute_bill since it changes the stock!

with an incorrect stock, the exercise can’t validated correctly

You seem to understand everything perfectly :slight_smile:


#3

okay Thanks . Commenting out the function call gave me the tick to go to the next exercise.


#4