Making a Purchase: Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5


#1

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 += prices[item]
        return total
        print total

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

Im dumb.


#2

The return statement is inside the loop, where it returns on the first iteration. Move it to the same indent as the for.

Anything after of return is unreachable, meaning your print statement will not execute.


#3

Ok, the indentation thing worked and I feel dumb even more cause I would never find it on my own.Thank you.


#4

Give yourself credit for trying, and for asking. In due course of time you will automatically look for this type of error.


#5

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