Making a Purchase


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

I have been stuck on this for two days. I try to solve the problem myself, hoping to learn from it, but i'm just not getting this one. I dont know what i'm doing wrong.

Replace this line with your 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 += food
    return total


We need to use item (which is nothing but a fruit name, string) inside of food object and use it as key for global variable prices, and add value of corresponding fruit's price to total.

We need to make changes there..

instead of total += food

use this template..

total += <access the fruit price value by passing food's item as key for prices dictionary>

Reading theory and instruction again might help too!


it wil be work ....

total += food[item]


In the for looping, you are not adding up the prices of the argument 'food'. How can sum up all the 'food' , when it is a list with strings?
You need to replace the 'food' in the looping context by 'prices[item]', as you need to add the prices of the food you chose.


Thank you so much for helping it make sense.


thank you for the help


Thank you for explaining it.


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