12. Stocking Out

#11

I don't understand why we need to change the value of the stock. This exercise needs to check, and not add items that are not in stock to the compute_bill total. Shouldn't we keep the values the same and let the code figure out that there's no apples?
Can you please look at my code and let me know what is wrong? It's giving the the message: "calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7" - Why is this adding pears to my list if Im calling compute_bill for items in my shopping list? Also, how can i subtract the item from stock?

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

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

shopping_list = ["banana", "orange", "apple"]

def compute_bill(shopping_list):
total = 0

for food in shopping_list:
if stock[food] > 0:
total += prices[food]

compute_bill(shopping_list)``````

Thank you,

#12

I have the same problem

#13

``````def compute_bill(shopping_list):
total = 0

for food in shopping_list:
if stock[food] > 0:
total += prices[food]

You have this extra return statement in your compute_bill function. What are you trying to do with that?

#14

I actually changed the code a bit, and I'm still not able to pass this lesson. Here is what it looks like now:

``````def compute_bill(food):
total = 0

for item in food:
if stock[item] > 0:
total += prices[item]

print compute_bill(shopping_list)``````

This is what I get: Oops, try again. calling compute_bill with a list containing 2 apples, 2 pears and 7 bananas resulted in 34 instead of the correct 30

This shopping list is a bit vague to me. What am I really trying to compute here? The total amount of bananas, oranges and apples? I am getting so many different answers. Super stuck... Help!

#15

The assignment is something like this: "You go to the store with a shopping list and try to buy everything on it. What price does the receipt say when you're done?" You can't buy something that isn't in stock and you can't buy more items than what are in stock.

I think that the issue here is that you (or well the test code) try to buy 7 bananas when there only are 6 in stock. Because of that it expects to not be able to buy the 7th one. Your code doesn't do anything to handle this situation. You need to remove items from the store as you buy them.

#17

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

Is this the way to handle the inventory? It's giving me the following error:
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}

Stocking Out - Any thoughts?
#18

Never Mind... This worked! Thank you all for the help!!! Moving on

#19

They expect us not to change the stock value while shopping.It's a problem at their end.So change the stock value according to the calling function.I did the same code in another platform,it worked perfectly without changing stock value.

#20

The problem is that you aren't supposed to call the function after defining it for this assignment, but many do it out of pure reflex or simply because they want to see if it works. So many headaches would have been avoided if the code behind the curtains would reset the stock-list before testing the function. Or an even easier fix; actually tell the user about this in the instructions

#21

what worked here? because i really don't see what the error message (i keep getting the same one) means.

#22

I removed the print statement from my code. You are not supposed to call it when you are done with the function. Check the last reply before yours here.
I kept trying to call the function when I was done to see it printed, but I was not supposed to do that.

#23

Exactly, thanks, I removed compute_bill(shopping_list) and the code worked fine !

#25

My code still doens't working :S and I already removed the print and changed the function
The error says the same thing

#26

#27

can you put that in code format please?

#30
``````shopping_list = ["banana", "laranja", "maca"]

stock = {
"banana": 7,
"maca": 1,
"laranja": 33,
"pera": 15
}

prices = {
"banana": 4,
"maca": 2,
"laranja": 1.5,
"pera": 3
}

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

I got that: Oops, try again. stock does not seem right! Be sure not to call compute_bill, since it changes the stock! It should contain: {'pear': 15, 'orange': 32, 'banana': 6 'maca': 0}

#31

When I was using -= and += it was not working for me either, so I re-elaborated it.

``````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 item in food:
if stock[item] > 0:
total = total + prices[item]
stock[item] = stock[item] - 1
print total``````

#32

This is my code for 10. 12. and I can't figure out what is wrong. Any help would be appreciated.
def compute_bill(food):
total = 0
for x in food:
if stock[x] > 0:
total = total + prices[x]
stock[x] = stock[x] - 1
compute_bill(shopping_list)
print total

#33

This is what the error message says:

Traceback (most recent call last):
File "python", line 26, in
NameError: name 'total' is not defined

#34

How is your indentation? I can't really tell the way you pasted here. You need to first remove the calling of the function compute_bill(shopping_list). This exercise does not ask for this and it will give you an error. Then you need to return `total` inside of your function so it's recognizes.
If you look at my code, you just need to replace where i used the word `item` for `x`. Your code should work with proper indentation.