Making a Purchase? Why my code don't work?


#1

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

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
total += prices[food]
return total
===========================
Dear all,

this is my code.
Is not it want us to make a function that could show the fruit price?
I don't understand where is this code wrong?

and most of us have show:

def compute_bill(food):
total = 0
for fruit in food:
total += prices[fruit]
return total

as the result answer. would someone tell me why?
why we need a for loop here in food? Is this mean this function can process multiple input?
thanks a lot :slightly_smiling:


#2

Food is the input for the function, you may be using stocks of items from other stores, so to make it compatible with all, you loop through food.