Compute bill


#1

then what will be the correct code for this ? iam also geeting the same error as

Traceback (most recent call last):
File "python", line 24, in
File "python", line 21, in compute_bill
TypeError: list indices must be integers, not dict

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 += food[prices]
return total

print compute_bill(shopping_list)


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

here:

total += food[prices]

lists (food) can only be accessed by index (0, 1, 2, 3 and so on), you use a dictionary as index? Please explain how that works


#3

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

print compute_bill(shopping_list)

is this correct ?


#4

customer comes to store to buy food (which we represent as a list)

then we need to get each item of food user wants to buy (for loop)

now we need to get the price of each item. item is a string, prices is our dictionary, how could we get value from prices using dictionary? We need to use a key (remember, dictionaries have key value pairs)

which variable contains the keys? (what do we need to get the prices of?)


#5

thank you sir. i got your point.

correct code will be this and it woks too-
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total

print compute_bill(shopping_list)


#6