11/13 - 11. Making a Purchase


Please, In need your help. All I want to know is what I'm doing wrong with detailed explanation of what's going on. 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

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        total = total + prices[item]
        return total


Hi @santao ,

The degree to which you have this indented ...

return total

... makes it part of the for loop. Therefore, it always returns a value during the first iteration of the loop, and misses all but one of the items in the prices dictionary. Remove one level of indentation so that it is no longer part of the loop.


Love you @appylpye.

Thanks for pointing out my error.

it worked:

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