Problem printing out the result using a function


#1

Here is the code :

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

print compute_bill(“banana”)

I d’ont understand why when I run the function with “banana” it doesn’t give me the total cost :confused: could someone help ? :slight_smile:


#2

The function expects a list, else it iterates over the string, character by character. To enter a single value, wrap it in [].

compute_bill(['banana'])

#3

oh fine thank you :slight_smile: