Checked everything, still isn't working


#1



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


Error that appears:
compute_bill[('apple')] resulted in a TypeError: list indices must be integers not 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
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        total+= food[item]
    return total


#2

You're trying to index a normal list food by strings. You probably want to use the dictionary (which you can index by string) prices instead.


#3

Is this what you meant?

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

It still shows an error, this time saying compute_bill[('apple')] returned 10.5 instead of 2.


#4

I'm sorry, but I don't see a change in your code.


#5

Sorry, accidentally pasted the wrong bit.. this is the new one:

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

#6

No, that's not it. You should still be looping through all food names that are given as a function argument, but to get their prices (and add them), you should be using the prices dictionary.


#7

So, like this?

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

#8

Tried it, worked. Thank you!


#9

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