Making a purchase


#1

Hii!! I am getting this error with my code. Please help. (making a purchase)
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 key in prices:
total=total+ prices[key]

    return total

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


Making a Purchase
#2

could you explain me why you loop over prices?

a function ends the moment a return keyword is reached, if the return keyword is reached in a loop, the loop will break to end the function


#3

really sorry.. couldn't follow.. please explain


#4

i asked a question about your loop, can you answer it?


#5

because prices is the dictionary which contains the keys and their prices.


#6

but the purpose of this exercise is to compute a bill of the consumer purchases. Which is stored in food (your function parameter)

so you need to loop over food


#7

It worked!!! thanks a lot!!
Is food a pseudo name for the list here ?


#8