Making a purchase problem



<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

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

<What do you expect to happen instead?>

I have no idea to be honest, instructions are unclear.


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

n = [4, 2, 1.5 , 3]
print sum(n)

<do not remove the three backticks above>

For X in food:
   print X

X is the dictonary key (apple, banana and so on), you want the value, you can do this with: prices[X], this will use the key, to access the price of this item in the prices dictonary


I don’t know what are you trying with code at the bottom

n = [4, 2, 1.5 , 3]
print sum(n)

for the error you need to use prices in for loop

total += prices[X]


i think he is manually adding the sum of cost of different items, which is never going to work since the exercise runs different test cases on the compute_bill function


That was desperation move, considering they added that in example I wanted to try it to see what happens.

Also in that instruction picture on that practice, there is a line:

for number in numbers:
total += number

should it be:

for number in numbers:
total += dir(number)

Please correct me if I am wrong.


The example is slightly different, so you don’t just copy it.

No, numbers is a list, so when you make the for loop, number will loop over the items in the list, and what is dir() for function?

The example doesn’t have a dictionary, you do have a dictionary.