def compute_bill(food):
total=0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total
where are the wrong?
def compute_bill(food):
total=0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total
where are the wrong?
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.
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
}
# Write your code below!
def compute_bill(food):
total = 0
for item in food:
if stock[item]>0:
total+=prices[item]
stock[item]-=1
return total
compute_bill(shopping_list)
def compute_bill(food):
total = 0
for x in food:
if stock[x]>0:
total += prices[x]
stock[x]-=1
return total
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.
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
}
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.
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]
return total
return total
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]
return total <------ Why?
return total
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]
return total
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.
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total = total + prices[item]
stock[item] = stock[item] - 1
return total
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}
Never Mind⦠This worked! Thank you all for the help!!! Moving on
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.
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
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.