以下是計算超市賬單的代碼。一切正常,但問題是,我被告知,如果輸入只是蘋果,這個解決方案將不起作用。爲超市計算賬單
我確實認爲蘋果的價值應該是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)
如果物品沒有庫存,您希望邏輯如何處理?這不是真的這個代碼的問題..只是...你想要它做什麼? – jdi
如果輸入是「[」apple「]',那麼這個人會說什麼呢? –
另外,您在'computeBill'中設置的代碼的方式,它會爲它們記錄整個項目的全部庫存。那是你要的嗎?如果我多次放入「香蕉」,我可以獲得兩倍的可用庫存。 – jdi