下面是计算超市账单的代码。一切都很好,但问题是我被告知如果输入只是苹果,这个解决方案将不起作用。
我确实相信苹果的价值应该是 0,因为苹果不在库存中,但我仍然相信有些事情我做的不对。请帮忙。
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)