Why the output is that?


#1



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

stock = {
    "banana":6,
    "apple":0,
    "orange":32,
    "pear":15
}
    
price= {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
def compute_bill(food):
    total=0
    for food in prices:        
        total = total+price[food]
        return total


Why the error message is :
Oops, try again. compute_bill(['apple']) returned 1.5 instead of 2


#2

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function still needs to ends, so the loop will break

so, your loop breaks in its first iteration, which is not what you want. Given now the customer ony pays for one product even if they buy multiply products


#3

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