Stocking out 12/13


#1

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

I'm getting this error and not sure what to do:
Oops, try again.
calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7


#2

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

return total

this worked for me :smile:


#3

You can do something like this also using list comprehension. You do have to have the supporting code for the list of prices for it to work.

MY CODE:

def compute_bill(food):
    return sum([prices[item] for item in food])

#4

Ahh... it worked when I change my statement if stock[key] >= 0:
to
if stock[key] >= 1

Thank you!