Lesson 11: Making a purchase: error: compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'


Hi, I have had a look at other posts where people are having the same issue as myself, however I don't follow what the solution is or where I am going wrong. Below is my code, with the last block of it what I am having trouble with. Any help would be wonderful.

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


You can't use operand + on strings and integers. You need to add prices[item] to your total


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

You need to use the operations as shown in "total + prices[item]"


Thank you, both of you. You helped solve it. I understand now :slight_smile:


I'm really struggling to understand what is happening here. Problem #1 is that I assumed I needed to format my code in a similar fashion as in the example (big mistake, ignoring the examples from now on).

I need to add each item that is added to the "food" list to the total. When we submit the code we see the error that OP has introduced, but when/how does "food" interact with the list "shopping_list" or any of the dictionaries? I understand lists and dictionaries, but functions and arguments are tripping me up a bit.

This is probably more difficult than it should be as I have to keep starting/stopping a lesson due to time constraints.


The names of function arguments are arbitrary. Think of them as placeholders within the scope of the function that substitute visually for the input values that will get crunched while the code is running. They can be named anything, so just use something that makes sense to you (and anyone else who might have to read your code later) within the context of the function.

For example in pseudocode not tied to any particular language:

define function foo(value)
    multiply value by 3

calculate foo(9)

In this example, the function is named foo, takes (value) as an input, and multiplies the input by 3. I fed it a 9 when invoking the function by putting (9) where it maps to (value) in the original function -- the 9 becomes its argument.

In the example itself, the function argument is named (food) simply because it makes sense within the context of the function -- it's easy to understand at a glance that you're crunching prices of the foods you're stuffing into the function. Strictly speaking, you can rename the argument to (x) or (list) or (your_mom) or anything else you like, so long as it remains consistent throughout the function.


Thank you for the explanation. Again, I believe I am overcomplicating matters. I am going to assume now that when we submit our code, the module inserts the proper argument into the function(s) we have defined in order to give us a meaningful result.

Otherwise I am just fixated on how to apply a function to the raw data found above it. Your example is crystal clear so thanks for that.