10.11 Printing total of items in prices dictionary


#1

Here is the dictionary and code given:

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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

I get the error message: "compute_bill(['apple']) returned 1.5 instead of 2".

Not sure why apple is giving 1.5 instead of 2 when it's defined as 2 in the dictionary.

(Side note: How do I indent code on this text interface?)


#2

CHange this line

for food in prices:

to

for food in food:

#3

That switch returns the error:

compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5


#4

change the name of the iterator to something other than food


#5

When i change it to "item" i still get the same error:

compute_bill(['apple']) returned 1.5 instead of 2


#6

Take a. Look at this link


#7

For me it worked in this way:

shopping_list = ["banana", "orange", "apple"]
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
    }

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

compute_bill(shopping_list)