Still having problems a day at the supermarket 11


#1

I am still having problems and no help lately:

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

error code:Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +: 'int' and 'str'
I've tried using += but no help i tried using food and item and total in different orders and still not working!


#2

item is a string (apple, orange, pear and so on), you should use item to access the prices dictionary to get the price


#3

@youcantstopit,

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

Each dictionary ( also called object )
has one or more key-value-pair's ( also called properties)
which are separated from each-other by a comma-,

The prices dictionary / object
has 4 properties
a banana property with property-key banana and its associated number Value 4
an apple property with property-key apple and its associated number Value 2
an orange property with property-key orange and its associated number Value 1.5
a pear property with property-key pear and its associated number Value 3

If you want to get at the associated prices Value of the "apple" property
you would use the syntax
prices["apple"]
or
in your case
prices[key]


#4

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