Trouble getting prices


#1

Hi, I'm having trouble with this one.
It is returning 7.5 for each answer and i cannot see why. When i hash out the For loop it returns the correct number but i get "compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list'"

here is my code (without the hashed out For)

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(item):
    total=0
    for item in shopping_list:
        total += prices[item]
    return total
    print total
    
print compute_bill("apple")
print compute_bill("pear")
print compute_bill("banana")

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


#2

here:

print compute_bill("apple")
print compute_bill("pear")
print compute_bill("banana")

compute_bill should have a list as argument, not string

furthermore, here:

for item in shopping_list:

you shouldn't loop over shopping_list. This will make your function only works for a single list. You included the function calls, those argument values get copied into the function parameter, so you should loop over the function parameter so your function works for any list supplied as argument at function call


#3

thanks, seemed counter intuitive to have For item in item.


#4

you can also change the parameter to food like suggested by the exercise, and then loop for item in food, for item in item is indeed a bit weird


#5

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