Need help, can anyone tell me what's wrong with the following code?Console shows 4 instead of 10.5


#1

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 of your loop


#3

can u please help me to correct it?


#4

you need to place return outside the loop so the whole loop can run and the function doesn't end in the first iteration/run of your loop


#5

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