Making a purchase bug


Though there are many thread on this none of them explains clearly and creates confusion . Someone please explain the calling function concept and how it working and also what is wrong in my code.

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):
for food in shopping_list:
total+= prices[key]
return total
print total

output id 117.0

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


Your function is supposed to loop through the strings in the list given to it, look up the price for each one, and add it to a rolling sum.
Your function is not currently doing this - which you can confirm by testing it yourself.
You can also use print statements to print out what is being done step by step to help you spot where the problem is.


Yes i know what i should do but i don't understand how it should be done as price should be multiplied with amount of stock. Anyhow.below is the correct solution and it works but didn't understood what does the last line do or if anyone can explain the code

def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total

print compute_bill(stock)


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