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


#1

Getting the error: compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +: 'int' and 'str'

What am I missing? My code:

food = ["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 = total + item
    return total

#2

@tortugavaldez,

With

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

you have your shopping list.

With

for item in food

you are picking 1 element a time from the food list.
In this case the FOR-IN loop will iterate 3 times.
iteration-1 item = "banana" -->stock[item] => 6 -->prices[item] => 4
iteration-1 item = "orange" -->stock[item] => 32 -->prices[item] => 1.5
iteration-1 item = "apple" -->stock[item] => 0 -->prices[item] => 2

As you can see, you do not have any "apple"'s in stock
so it would be nice if you use a test-on-availability
with an IF statement
like

if stock[item] > 0:

we can then increment total with prices[item]


#3

I tried that and still give me error, because there is no stock for apples!
So, I finished my code writing: total = total + prices [item]
and works. thanks! you give me a clue.


#4

hi everyone. i think i solved this problem. i think there is a mistake at the instroctions because the right answear is this:

Write your code below!

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


#5

continue:
there is no need of a "food" list because there is already one which called:" shopping_list " and the fonction takes the items fron it and calculates only the values from it!!
hope i helped.


#6

yeap...work's like this. thx


#7

yes this is the right answer. the instructions say to pass an argument called 'food' but I just need to re use the shopping_list[]