Making a purchase


#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 item in food:
total += item
return total

It keeps saying compute_bill([‘apple’]) resulted in a TypeError: unsupported operand type(s) for +=: ‘int’ and ‘str’ but I don’t know what that means or how to fix it please help


#2

item is a product, for example "apple", which is a string

string + integer = not a happy marriage, as you can tell from the error message

you need to add the price of item to total, maybe prices dictionary can help?


#3

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