Stocking Out - "Resulted in 0 instead of the correct 7"


#1



Stocking Out


Hello - I've been going around and around on this and finally need to "phone a friend". Can someone help clarify where my code is going wrong? Thanks!

Error msg:
"Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 0 instead of the correct 7"


I thought the code would run without issue and stock would be debited/reduced


Repshopping_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):# Write your code below!
    total = 0
    for x in food:
        if stock[x] > 0:
            total += prices[x]
            stock[x] -= stock[x]
            print stock[x]
        return total


#2

The return line should not be in the loop or the if.

That line won't be expected.


#3

Thanks for the quick response, MTF - so I've removed the unexpected line & pushed the return to outside the loop, but now am getting:

"Oops, try again. calling compute_bill with a list containing 2 apples, 1 pear, 1 orange and 5 bananas resulted in 8.5 instead of the correct 24.5"


#4

Will need to check the lesson to be able to further consider this question. Please post a link to the exercise. Thanks.

From the looks of the error message, it would seem we are not to be meddling with the stock, just yet, and only write,

for x in food:
    total += prices[x]
return total

Now the lesson checker can send whatever list it has in store.


#5

Ok - will do. Here's the link (hopefully this is what you needed):

Thinking that the original values are changing (and the test values are changing), I've reset the code and re-inserted my code in but keep getting this error now:

"Oops, try again. calling compute_bill with a list containing 1 apple, 3 pears, 2 oranges and 5 bananas resulted in 8.5 instead of the correct 32.0"

from the following 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):# Write your code below!
total = 0
for x in food:
if stock[x] > 0:
total += prices[x]
stock[x] -= stock[x]
return total

food_list = ["apples","cumquats"]


#6

Exercise link, please, not the gist.


#7

sorry - first time poster, mtf. This?

https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3


#8

NP. Okay, I see that we are indeed expected to subtract 1 from stock and only add to total when stock is 1 or more.. That was my question, best answered by the nstructions. For future, please include a link in your question, allowing everyone to be on the same page. There is so much similarity between exercises that often it is hard to find the right one. Link cures that problem for us all. Thanks, btw.

So the lesson checker has a long list of items,

['apple', 'pear', 'pear', 'pear', 'orange', 'orange', 
'banana', 'banana', 'banana', 'banana', 'banana']

apple   =>  0
pears   =>  9
oranges =>  3
bananas => 20
--------------
           32

So now test that list in your code.

This is wiping out the stock on the first pass. Each item only gets added once; hence, 8.5

stock[x] -= 1

would be the way to go with that line.


#9

Thank you! That sorted me out.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.