Stocking out - Wrong Total


#1



I'm stuck on Stocking Out


First iteration succeeds and the total is 7 when buying 1 apple, 1 pear and 1 banana
On the second iteration I'm getting the wrong total of 8.5


Total is meant to be 33.5, when buying an additional 1 apple, 3 pears and 5 bananas.

Something about repeating and adding the total to the previous total I'm not understanding, I think.


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

print total


#2

Your problem is very simple:
Have a look at this part of the instructions.

If the item is in stock and after you add the price to the total,
subtract one from the item's stock count.

So why are you subtracting stock[food] from stock[food]? That returns 0! Instead of this, remove 1 from stock[food] in your line 24.

stock_food = stock[food] - 1

Also you should remove that 'print total' statement from line 27.


#3

Cheers, I was subtracting 1 before.. must have got frustrated and changed it around.


#4

No problemo! :slight_smile:


#5

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