13/13 Let's Check Out


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/lets-check-out?action=lesson_resume&link_content_target=interstitial_undefined

<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 21, in compute_bill
KeyError: ‘b’

<What do you expect to happen instead?>

It doesn’t print to console. I want to 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 += prices[item]
      stock[item] -= 1
  return total

print compute_bill("banana")

#2

The function takes a list, whereas you are passing in a string. The string is iterated the same as would be a list, so the first value is b which does not exist as a key in stock dictionary.


#3

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