Please help me friends ! Thanks in advance :)


#1

This is the code I have written.

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 item in food:
total= total + item
return total
n=[1,2,3,4,5]
print compute_bill(n)

I am doing exactly what is mentioned in instructions. The function is giving the answer but I am getting this error message.

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

Please let me know where am I missing the point?


#2

Consider, item is a string in each state of the iteration. total is defined as a number. When we go to add a string to a number the compiler chokes. So we need to point to the value associated with the key (item).

        total += prices[item]

Notice that we referred to the prices dictionary for pertinent data in obtaining our total?


#3

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

I wrote this quote and the answer is coming .
But again this error is shown.
Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str


#4

prices is one of the data dictionaries, so won't be passed as an argument. We should have a shopping_list or groceries_list to pass into the function.

shopping_list = ["banana", "orange", "apple"]
#...
print compute_bill(shopping_list)

The items in food (the shopping list) share key names with the two data dictionaries. This allows us to query either dictionary with a key from food.

        total += prices[item]