Making a purchase


#1



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


My code keep returning the error 'Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5'


I need this to return all the prices in a sum


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 i in food:
        total = total + prices[i]
        return total


#2

The return function is the problem. You should be returning the value outside the for loop

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


#3

I think it has to do with the indentation, yep! This worked for me:

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

#4

you have to pay close attention to indentation in this language


#5

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