This forum is now read-only. Please use our new forums! Go to forums
Shopping Trip ! I have a problem,please help me!
groceries = [“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 food in prices: if stock[food]>0: total= total+ prices[food] stock[food]-=1 return total Oops, try again! Your total is too high. Make sure you are properly decrementing the stock count.
Answer 51ebaedb282ae3b0d1007ac0
Hi,
You forgot for stock checking (if stock[food] > 0)
`def compute_bill(groceries): cost = 0 for food in groceries: if stock[food] > 0: cost += prices[food] stock[food] -= 1
return cost`
Answer 51ef6dff282ae339d50021ee
def compute_bill(food): total = 0 for s in food: if stock[s] >= 0: print “buying one %s, there are %d remaining in stock” % (s, stock[s]) total+= prices[s] stock[s] -= 1 return total
print compute_bill(groceries)
To pass
Answer 51f83115f10c60b18b0000ff
Hi everybody, I tried all the functions that you have posted but any of that worked, if someone discover a solution for me, I’ll be very pleasured.
Answer 51e8c67d52f863cf5600058a
I cant get past this problem either and I have the same exact code as Garrett. Also this is 3/4 as far as I can tell where you have to subtract the totals from the stock dict as well. I never got to the point of figuring out how to subtract because it tells me the cost is too low even though it was correct in the previous exercise… shrug
def compute_bill(groceries):
cost = 0
for food in groceries:
cost += prices[food]
stock[food] -= 1
return cost
1 comments
consider when your return statement is executed, if you were to give your function a spin you’d find it only computes cost for the first item in the list
Answer 51ed958a282ae3f418001dde
I figured it out. Just use “Burkes’” code up there ^^ but make sure the return statement is aligned with the for loop statement :)
Answer 53f6c4ba52f86316500002f2
I figured out how to pass it. It is related to the way you name your variables. The code below the line should work.
item = [“banana”,”orange”, “pear”, “apple”] prices = {“orange”: 1.5, “pear”: 3, “banana”: 4, “apple”: 2 } food = {“banana” * 3, “orange” * 3, “pear” * 3, “apple” * 3 } def compute_bill(food): total = 0 for item in food: total += prices[item] return total
print compute_bill(item)
Answer 51e843037c82ca28e2002433
def compute_bill(food):
total = 0
for n in food:
total += prices[n]
return total
He’s basically saying he has no idea what’s wrong with it, but still decides to reply even though he just states the obvious.
But anyway, you’re overcomplicating things jusst a litttttle bit :) you don’t need to worry about stock in this exercise, so you can remove that for less confusion! You got things a little bit mixed up towards the beginning. Just use the code that worked for me as reference once you’ve removed the stock bit. Hope this helped!
5 comments
it might also be that when people start asking a lot i try help them help themselves, doing what i said either solves it or makes you bump into a line of code you understand which you can then look up, test or ask about
especially when they don’t format their posts properly
for ‘Making a Purchase’ you don’t need to worry about stock, but for the next one - ‘Stocking Out’ you do, since the question doesn’t say which one it is, i have a hard time understanding how you came to the decision that it is the one where you don’t need to take it into consideration and start making claims about it being over-complicated
the code doesn’t work because it completely disregards what groceries you pass to it as an argument, you failed to see that yet you’re saying i don’t know what’s wrong and you start saying it’s the former exercise instead.
also able to spot that the code matches the instructions of next exercise
Answer 51e8d4837c82ca8fdf0001a8
so, Garret3412, here’s step one and two of my post, perhaps you can continue since i have no clue how to:
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 food in prices:
if stock[food] > 0:
print "buying one %s, there are %d remaining in stock" % (food, stock[food])
total = total + prices[food]
stock[food] -= 1
return total
groceries = ["banana", "orange", "apple"]
compute_bill(groceries)
note the extra print statement in compute_bill(), run the code and watch what it does
1 comments
This assumes we are buying ONE of each item in the list. This is just an assumption. The question doesn’t specify quantities purchased
Answer 51e75d2d8c1ccca3760003f4
pretty sure i already explained to you how to format code on here, here it goes again; to preserve formatting when you post code; highlight it all and click the {}
-button in the post editor
how to debug:
find an input for your function that causes it to respond incorrectly
insert print statements in your function the print the variables in there to give you an idea of what’s going on inside the function
judging from the output of your prints, locate where/when it starts going wrong(compare output with doing the math manually)
identify which piece of code is causing it
fix it
1 comments
thank you
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
I put the stock checking