Making a purchase problem


#1


https://www.codecademy.com/en/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'

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)


#2

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


#3

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]

#4

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


#5

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.


#6

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.