Below is the code to compute the bill for the supermarket. Everything is ok but the issue is that I am told that this solution would not work if the input is only apple.
I do believe that the value of apple should be 0 since apples are not in the stock but still I believe there is something that I am not doing correct. Please help.
groceries = ["apple","banana", "orange",]
stock = {"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def computeBill(food):
total = 0
for item in food:
tot = prices[item] * stock[item]
print item, tot
total += tot
return total
computeBill(groceries)
I am just going to go off on my own with this answer and make suggestions, since it seems the specifications for your
computeBillfunctionality are not well defined.If the items are not in stock, and your instructor says it is not acceptable to return
0in this case, then your other options are to raise an exception, or a sentinel value indicating an error state.Or if you don’t want to raise an exception, you could return
-1if you feel that is not a valid total anyways:There are some other problems with the function in how it calculates the list vs stock, but you said you didn’t care about those right now.