# Making a purchase: wrong answer returned

#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 food in prices:
total += prices[food]

I get an answer of 1.5 and not 2.

Seems I got the answer for oranges not apples

#2

One of the instructions states

"02. For each item in the food list, add the price of that item to total."

This gives you a hint that your `for` loop should be something like

``for item in food: #where food is a list (input)``

Since item is the index, total becomes

``total += prices[item]``

Also, move your `return total` back so that it lines up with your `for` loop statement.

#3

``````# Write your code below!
def compute_bill(food):
total = 0
for item in  prices:
total += prices[food]

is wrong

but

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

is right

Whats the difference?

#4

food is a list, not an index hence why `price[food]` doesn't work. Also, the list you are inputting uses the parameter food and not `prices`. If you have

``````def compute_bill(food):
total=0
for item in prices: #changed food to prices
total += prices[item]

you're basically going to ignore your function input, `food`, and just get the total for your prices list.
eg.
Say you have

``````print compute_bill(['apple'])
print compute_bill(shopping_list)``````

both will return

``````10.5
10.5``````

which is the total for prices. You can always change little parts of the function and see what error/results you get so you'll understand what's going on

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.