Trying to grasp compute_bill


#1

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/making-a-purchase?action=lesson_resume&link_content_target=interstitial_lesson

error says: compute_bill([‘apple’]) resulted in a TypeError: unhashable type: ‘list’

Why doesn’t it accept the def compute_bill as is? I’m looping all of the items in prices…why can’t I just word it like that instead of using 2 undefined (in my code) terms?



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(food):
  total = 0
  for item in prices:
    total = total + prices[food]
  return total



#2

Add pear to your shopping list,

and add item to your total not your argument food,


total = total + prices[item]

Hope this helps :grin:


#3

its important to understand what the value of food is:

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

print compute_bill(shopping_list)

so food is a list of products the customer wants to buy, so why do you loop through prices?

now you know that food is a list, you should also realize that:

prices[food]

throws a TypeError, we can’t access dictionary with a list key


#5

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