Loop is not going through all items?


#1



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


My code is printing only the price for the first item in my shopping list. When run as it is below it always prints out "4" when I run compute_bill(shopping_list)


Instead, it should be adding together the prices for each of those items listed in shopping_list and printing "10.5"


shopping_list = ["banana", "orange", "apple", "pear"]

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)


#2

Hi @staylow ,

Check the indentation of your return statement. Do you really want it to be part of the for loop?


#3

thank you so much! I misunderstood the purpose of return. Knowing it only needs to be done once to spit out the number, I moved the indentation to make it part of the function rather than the loop. Problem solved!


#4

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