Making a purchase lesson help


the error is "compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5" and I cant figure out the mistake I made.

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5, 
    "pear": 3

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


What operations would it need to have done to reach 10.5? How can you find out if it did those? Can you make it tell you what it does as it runs? And if so, where did it go wrong?


Add all of the prices when you insert all four types, but it always comes out with four. I think it is just running it through for banana, but I can't see why.


this had the answer: apparently the return cant be indented inside the for loop or else it terminates the function.


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