Making a purchase | A day at the supermarket


#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
}

def compute_bill(food):
    total = 0
    for item in food:
        total += item
    return total

compute_bill(['apple'])

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!


#2

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


#3

...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?


#5

total += prices[item]


#6

Because item can be everything* in your code


#7

OMG U R GOD. HOW R U SO GUD! wow.


#8

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


#9

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


#10

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


#11

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


#12