Exercise 11/13 - A day at supermarket


#1

Hello,

I think I'm completly missing the point of this exercise.. Can someone help me??

I keep getting the error

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

shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

what I had in mind:

 def compute_bill(food): 
          total = 0
          for i in food:
               total = total + i
          return total

#2

I'm having the same problem.. and what happened to Q&A?


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

#4

Logic...

Thank you for the help!


#5

we have to add price of the item to the total that's why we are using prices[item].
If we add only item to the total total = total + i that is not meaningful.

total = total + prices[i]
In this way we add the prices of item to the total.


#6

Thanks for the help :smile:


#7

thank you bro...but is it the correct code??


#8

i have that code:
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 key in food:
total = total + food[key]
return total

Write your code below!

print compute_bill(stock)

the system sey me "Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str". What is my problem?