Traceback Error: Let's Checkout 13/13. Trying To Print Result


I can’t find my error. Please help me. I’m supposed to see the result of the compute bill function on the shopping list instead, I get this:

Traceback (most recent call last):
  File "python", line 27, in <module>
  File "python", line 21, in compute_bill
KeyError: 's'
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 += prices[item]
      stock[item] -= 1
  return total
  print total



you shouldn’t call the function, it causes problems with the exercise validation.

even if you want to call the function:


make sure to supply a valid argument, why is shopping_list a string here? As string, shopping_list is an invalid argument.

Now for item in food will loop over the letters of shopping_list string, none of which are valid key value for your dictionaries, which will throw an error


do you need help ladarva?


Yes. Thank you. I know I’m not supposed to call that function with shopping
list as a string. Don’t even know what I was thinking.


it should have been the variable indeed, given it needs to be a list of products the customer wants to buy

Did you resolve the issue?


Yeah. Thanks a lot. It was my silly mistake that caused the problem.