Stocking Out


#1



I'm not sure what is the error. Can someone help me?


My result is different than what it should be.
i.e. 1 Apple, 3 Pears, 1 Orange and 9 Bananas results in 46.5 instead of 34.5.


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):
    total = 0
    for item in food:
        if stock[item] > 0:
            total = total + prices[item]
        elif stock[item] == total:
            count = stock[item] - 1
            
    return total


#2

Do you agree with the error message?

If so, execute your function manually or add prints in it saying what it does, compare to what it should do.

If not, then out of that there are 3 pears, 1 orange, and 6 bananas available to purchase which adds up to 34.5


#3

hi there, you could try mine.
def compute_bill(food):
total = 0
for item in food:
if (stock[item] > 0):
total = prices[item] + total
stock[item] = stock[item] - 1
return total


#4

This should work

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 x in food:
        if stock[x] > 0:
            total = total + prices[x]
            stock[x] = stock[x] - 1
    return total

#5

It doesn't work. I have almost exact same code as yours and exact same problem as the author of this topic.

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

It's like the stock doesn't change. It's only 6 bananas in stock so if I want to buy 8 of those the code should only count 6. But it counts 8. I'm confused. What else might be wrong?

EDIT:
Here is the solution. Apparently it's related to some kind of bug.


#6

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