Making a Purchase error code dict is not callable?


#1


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096

I keep getting the error message . "Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable"

Any ideas why this might be happening? I've compared my code to a bunch of other corrected responses on this question and I'm just not seeing where mine is going wrong.


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 += prices(item)   
    return total


#2

Oh my gosh. I looked at other code for like 2 hours, posted and immediately saw what was going wrong. I used parenthesis instead of brackets for accessing the prices list. I wrote prices(item) where it should have been price[item]


#3

This code:

prices(item)

looks like a function call, this is the reason of the error.

To retrieve data from dictionary you have to use:

prices[item]

#4

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