11. Making a purchase


#1



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

Whats wrong?

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 compute_bill(shopping_list)


#2

a function ends the moment a return keyword is reached

so your function ends in the first run/iteration of the loop

but to calculate the correct total the whole loop needs to run


#3

I got the solution, thanks, it was just a space.


#4

the space (or rather, indent) does make quit a difference to how your program behaves.


#5

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