11. making a purchase


#1

Please help. I've read the comments above but am still having no luck!

I'm constantly getting the error below:

compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

This is my 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
}

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

print compute_bill(shopping_list)

11 Making a Purchase
#2

a function ends the moment a return keyword is reached, in your case this happens in the first run of the loop

Change the indent of return, so the whole loop can run first before returning


#3

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