Stocking Out - Answer


#1

This took me awhile to figure out, I think there's a bug on this lesson because I swear I tried this with a couple different codes that should've been correct. But this works:

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 = total + prices[item]
            stock[item] = stock[item] - 1
    return total

#2

i think your code is right
maybe you should try refresh ?


#3

I came up with almost the same code (independently, didn't copy you), and CodeAcademy interpreted it as correct.

try total += prices[item]
instead of total = total + prices[item]

They are the same thing but who knows the console might like it better.


#4

I had a similiar issue. Looking at your answer I realize my answer was correct, but I to delete
the print command at the bottom before it passed as correct


#5

That's it right there masterstroggo. Had the same code as OP, but still had print calling the compute_bill function. Remove that and the code works.


#6

It's not just the printing piece. Remove any calls to the function. Apparently this lessons just wants you to modify the function, not call it.

The instructions should explicitly state this.