# 12. Stocking Out

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

where are the wrong?

3 Likes

Please explain what is happening different from what you expect so that we know what to look for. Also take care to get the formatting of your code right, what you posted isnāt valid Python code.

3 Likes

Iād like to agree with @ionatan about your formatting. Without it itās almost impossible to know what youāve gotten wrong.

Did you check to make sure that your return statement was outside of the for loop?

``````what's wrong with my code?
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 item in food:
if stock[item]>0:
total+=prices[item]
stock[item]-=1
compute_bill(shopping_list)``````
7 Likes
``````def compute_bill(food):
total = 0
for x in food:
if stock[x]>0:
total += prices[x]
stock[x]-=1
7 Likes

Hi,

When we say

how does python recognize that the xās are the items in shopping list (or) how does python decide what food is? I am asking since food is not defined before the function. Thanks.

2 Likes

Guys change the stock value for each of the values in the calling function.
Like This
shopping_list = [ābananaā, āorangeā, āappleā]
stock = {
ābananaā: 7,
āappleā: 1,
āorangeā: 33,
āpearā: 15
}

7 Likes

thanks, pyywhiz04125, that helped! must have reduced the stock with the first (probably wrong) try.
the codes for the function from betarunner12266 and gmanu are both right.

1 Like

Yesā¦their code also correctā¦itās just the stock value

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,

I have the same problem

``````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?

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!

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.

1 Like
``````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}

3 Likes

Never Mindā¦ This worked! Thank you all for the help!!! Moving on

1 Like

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.

1 Like

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

13 Likes

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

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.

1 Like