Making a purchase


#1

I keep getting an error for the type referencing apple. I didn't change any of that code...

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:
        total += item
    return total

Making a Purchase
#2

what is item?

it is a product (in your case apple, so you need to use item as key to get the price value from the price dictionary


#3

I keep getting:

Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'list'

this is what I changed:

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

#4

where do you have the price of the product? in your prices dictionary, so you need to use item as key to get the value from the prices dictionary


#5

instead of ( total += item)
try total += prices[item]
it may work


#7

total will only print the string in the list(shopping_list) if it's parsed in as an arg. you have to specify an int to add to the total... hence prices[item] which is a string. eg prices["banana"] = 4


#8

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