11/13 Making a Purchase


#1

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

compute_bill([‘apple’]) resulted in a TypeError: unsupported operand type(s) for +=: ‘int’ and ‘list’

I’ve tried following the instructions and doing a bit of reiteration, but it has done little. Could anyone shed light on this matter?

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 x in food:
    total += food
  return total

#2

food is a list, maybe adding a function call will clarify what food is:

def compute_bill(food):
  total = 0
  for x in food:
    total += food
  return total

print compute_bill(shopping_list)

now, food (list) + total (integer) results in an error, we need to add the price (we could use the prices dictionary for this) of each item (loop) in food list to total


#3

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