Facing problem for A Day at the Supermarket under 11. Making a ourchase.. please help


Please help understanding what is wrong in following code. It is executing and providing the result however site is throwing error as "Opps, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers , not str". Here is the 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 = total + food[item]
return total

print compute_bill(prices)


well, here:

print compute_bill(prices)

you give prices as argument at function call, however what the customer will buy is in list form, for example:

print compute_bill(shopping_list)

so food will contain lists, which gives an error here:

total = total + food[item]

given food is a list, and item a string

you want to get the price of item and add it to total


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