Code returns [0,2] instead of 2


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096

The error says that the code returns [0,2] instead of what it should be, which is 2.

The code should be able to function with any "shopping_list".


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.append(prices[item])
        sum(total)
        return total


#2

first you should stock the sum total to a variable let's use total so at end of the loop we will return total( your indent for return total is not correct ,should be at the same level of for item in food: and total = [0]) , our code can be

def compute_bill(food):
    total = [0]
    for item in food:
        total.append(prices[item])
        total=sum(total)
    return total

#3

total should be equal to []