A Day at the Supermarket


#1



A day at the Supermarket


my code seems okay but i always get an error

Oops, try again. calling compute_bill with a list containing 3 pears, 3 oranges and 8 bananas resulted in 45.5 instead of the correct 37.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
}

# Write your code below!
def compute_bill(foods):
    total = 0
    for food in foods:
        if stock[food] > 0:
            #print food + str(stock[food]) + " " + str(prices[food])
            total += prices[food]
            stock[food] - 1
            total
    return total
    
print compute_bill(shopping_list)


#2

this:

stock[food] - 1

will not reduce the stock, you need to update/re-assign stock[food], how do you think you can do this?


#3

like this?

stock[food] = stock[food] - 1

i still get error :frowning:


#4

yes, like that.

remove the function call:

print compute_bill(shopping_list)

it influences the stock before the exercise can do the validition.


#5

thanks! it finally worked :slight_smile:


#6