SOLVED - 10.11 Making a Purchase: TypeError 'str' object not callable


#1



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


My code - found in the block below under # Write your code below! - throws the following error message:

Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'str' object is not callable


I expect it to run the loop to return the sum of the prices. though I've tried changing the code, I haven't been able to interpret the error message correctly to find the right fix.


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


#2

Hi @alethinophidia,

Check this line for the problem ...

total = total + item(prices)

Consider what is the correct syntax for accessing one of the items in the prices dictionary ...

  • What is the key?
  • How should it be specified to access an item?

#3

ahh, this syntax passed:

total = total + prices[item]

Thanks, @appylpye, those were good clues. I revisited the lessons below and it became clear:

1.) I was using parentheses rather than brackets, and
2.) I transposed the call command.

I was essentially using gibberish in trying to call keys inside a dictionary, but I'd flipped the dictionary inside out.

• Python Lists and Dictionaries.This Next Part is Key
https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/2/1?curriculum_id=4f89dab3d788890003000096

• A Day at the Supermarket: This is KEY!
https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/0/2?curriculum_id=4f89dab3d788890003000096


#4