Need Help Stocking Out


#1

I have been lurking on the board for a while and have completed my code correctly in my eyes and have placed my code in an actual Python environment to verify I am not going insane. However, when I try to run the same code here I receive the error message "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}"

Can somebody help me?
Here is my code:

shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 2,
"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

Thank you.


#2

1

Re-edit your Post
selecting your code
then =click= on the </>-symbol-of-this-editot
your code is then in a so-called pre-code mode
and you are able to do the proper indentations

2

Your stock should be

stock = {
    "banana": 7,
    "apple": 0,
    "orange": 33,
    "pear": 15
}

#3

place the return total statement at the end of function definition and not at the end of the loop.
Hopefully, it will solve your problem.


#4

This is my code:

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

It looks identical to yours spelling wise, so I'm guessing there's a indentation error in your code.


#5

The question:
Must we not check on stock[item] >=1 ??


#6

I think since the fruit number must only be natural numbers, the conditions ">0" and ">=1" are interchangeable. Thank you for the question sir!