What is wrong with my code?


#1



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


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


I think that my code is completely viable, and I have tried a number of small variations on it, but I continually get the error stated above. Here is my code, please help me understand what I am missing.


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[str(item)]
        return total


#2

a function ends the moment a return keyword is reached

so, your function ends in the first iteration/run of your loop, so it will never get the correct total for a food list which contains more then one item


#3

Thank you so much. I guess this is a good lesson that one indentation does matter in Python!


#4

Indention is very important in python, where in many language its mostly for readability, in python indent has a much more important function


#5