Making a Purchase


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


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


It should run correctly and allow me to get to next exercise.


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

print compute_bill([1,2,3,4,5])


#2

except the argument at function call won't be a list of integers, it will be a list of products the customer wants to buy:

print compute_bill(shopping_list)
print compute_bill(['pear', 'orange'])

so item will be a string, string + integer is error as you experienced

you will need to retrieve the price of item, maybe you can use your prices dictionary somehow? I will leave that for you to figure out


#3

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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

print compute_bill(prices)

i tried to figure out what you were suggesting but now its giving some other error.

Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str


#4

no, you can't change the arguments at function call (those are fixed, given the functions purpose and exercise validition), you will need to change the function so your function works for this function calls:

print compute_bill(shopping_list)
print compute_bill(['pear', 'orange'])

the exercise has several of such test cases, the argument at function call will always be a list of products (which represents a customer buying products at your store)


#5

You mean i should concatenate string and not initialize total=0


#6

no, you should change the function:

def compute_bill(food):
    total=0 
    for item in food:
           # add price of item to total here
    return total

print compute_bill(shopping_list)
print compute_bill(['pear', 'orange'])

#7

Got it , Thanks a lot


#8