Need help 11 making a purchase


#1

Ok so here's the low down I need help with the end of my code here
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 total in food:
total += food
return total

whenever I press save and continue it leaves me with this Error
Oops, try again. compute_bill(['apple']) resulted in a TypeError: cannot concatenate 'str' and 'list' objects

Got no Idea what it means


Making a purchase help
#2

Let's break this down.

The input will be assigned to the variable 'food'
Assign the variable 'total' to = 0
For 0 in food add it to 0

Well there's our first bug, but let's say we fix that and instead of using the variable 'total', use a new variable.
Remember - a for loop says "for each item in this argument" or in this case - "for each item that we are given as input" (we assigned the input to the variable 'food')

So we change the for loop to:

for item in food:
total += item

for item in food add it to 0

Do you really want to add 'apple' to 0?
Maybe it would be better to add the price of apple to 0


Making a purchase help
#3

oooooooooh thanks, kind sir or madam I really appreciate that I would have been stuck on that for hours. You made my day Merry Christmas Eve.


#4

Im sorry, I just dont get it. Can someone post the answer? I dont know how to get the price of the item. item[price] does not work? I dont know.

Update: Ok I figured it out.

Here is the answer for anyone who is stuck and can't seem to get it. As sicklad said, we're trying to add the price to the total. In order to do this, we have to call the "item" that we are using as an argument from the "prices" list since that's where their prices are located. So your end-code should look something like this:

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