Let's Check Out!


#1

https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/lets-check-out

<In what way does your code behave incorrectly? Include ALL error messages.>
Traceback (most recent call last):
File “python”, line 26, in
File “python”, line 22, in compute_bill
TypeError: unhashable type: ‘list’

It should compute the total and print



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:
    if stock[item] > 0:
      total = total + prices[food]
      stock[item] -= 1
  return total

print compute_bill(shopping_list)



#2

here:

total = total + prices[food]

prices is a dictionary, food is a list. You attempt to access dictionary with a key of type list, that throws an error. What should be changed here?


#3

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