12 stocking out a day supermarket

#1

This is my code!

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

compute_bill(shopping_list)

The message I got is'Oops, try again. calling compute_bill with a list containing 1 pear, 2 oranges and 8 bananas resulted in 38.0 instead of the correct 30.0.'

I think 38.0 is the correct answer.
Please tell me what should I do and what the number ,30.0, is!

I'm not a native.speaker. Maybe only I can not understand the instructions.

#2

Hello @coursewhiz71998,

I had the same problem at that point in the course. The thing is, Python doesn't expect you to check the stock before computing the bill. So you should just remove the if statement checking the stock of the item and the decrement of stock after `total = total + price`. Try that and reply if it doesn't work....

#3

def compute_bill(food):
total=0
for x in food:
price= prices[x]
total=total +price

Is this the code you said?
This code doesn't work,either.
I got the message, '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 '

#4

Well @coursewhiz71998,

It hasn't been the first time I have been wrong. I thought you were referring to the previous exercise: A day at the supermarket 11, but you are referring to number 12. In which case, I think your original code is correct except for one thing. You shouldn't make a variable st and set it to stock[x], you should directly decrement stock[x], it is because of this that your original code doesn't work.

Hope this fixes it up.... Sorry again

#5

Hi. Below is my code for A Day at the Supermarket. Code seems to be correct, but still getting error.can anyone correct and tell me.

#6

This just worked for me, shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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

print (shopping_list)

#7

Sorry it is not working for me. ****compute_bill("apple") should return 2** according to the error message I am getting.

#8

Check this snapshot and kindly suggest me the right code.

#9

I just had this issue and double check your return statements. You need two.

#10

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

Is this the code you said?

I got the message, "Oops, try again. 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}"

#11

I'm getting the same error, but the instructions state we need to change the stock. Or am I missing something?

#12

I think I just figured in out. Write the def compute_bill but don't run it yet

#13

It seems the exercise doesn't want you to run the compute_bill function

#14

I also thought we must not change the stock directly. So I wrote "st=stock[x]"

I think my first code is the closest to the answer.
How do you feel?

#15

Check, my second comment, once I removed the part where I actually call the compute_bill function it worked

#16

Thank you very much!!!!!

m(__)m

#17

É só remover a chamada da função

#20