Making a purchase | A day at the supermarket


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

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


resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

I am not entirely sure what that means, but when CodeCademy runs compute_bill it gets an error. If someone could explain what this error message means, that would be great. Thanks!


item is a product, in your case 'apple' (which is a string), total is an integer, and + is an operand (adding), now read the error message again. You can't add a string to an integer.

you will need to get from item/product to price since compute_bill needs to calculate the bill for the customer

maybe you could use your prices dictionary somehow? Go think about it


...So I have to get whatever number is assigned to an item and add it to total? How am i supposed to reference the number?


total += prices[item]


Because item can be everything* in your code




i actually wanted you to reach this conclusion on your own, i believe it would have taught you more


I just realized how easy the answer was. Im really mad now.


once you see the solution, it always looks easy. But getting to the solution is trickier then it looks, which is frustrating


A post was split to a new topic: Making a purchase