A day at the supermarket error I don't get?


#1

Hi, I've been getting an error at this exercise :

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 i in food:
total+=i

Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

I am confused as I have only defined the function in my code, and not called it. Why is the error as if I called the function on a dictionary instead of a list, when I haven't called it at all?

Thanks!



Replace this line with your code.


#2

It calling your function to check does it work, its done in background. It's not working :smiley:
You want to add prices to your total


#3

@devslayer38165,

With

for i in food

you will iterate through a shopping-list
and i will represent 1-food-item
of which you can get the cost-price by using prices[i]


#4

Yes but I did not call any function, I just defined it. I did not troubleshoot it yet as the error confuses me.

The function got called in the background on what list argument? And why was it called without me? :stuck_out_tongue:


#5

Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'

compute_bill(['apple'])
this is called. Thats some form to check if your code is valid


#6

Ok, got it, I was supposed to get the prices in there, which was cool, except the requirements were a bit confusing and the function was called in the background.

for i in food:
total+=prices[i]
return total

Thanks!