【发布时间】:2014-06-30 03:06:48
【问题描述】:
我想要做的是在 python 中创建一个函数来将添加到购物车的物品的总价格相加,如果该物品在股票字典中可用。同时,如果该项目可用并添加到总数中,我想将该字典中的项目数量减少添加到购物车的数量。
我完全卡住了,我不知道如何前进,请看下面的代码:
stock = {
"item1": 6,
"item2": 0,
"item3": 32,
"item4": 15
}
prices = {
"item1": 4,
"item2": 2,
"item3": 1.5,
"item4": 3
}
def compute_bill(cart):
total = 0
for key in cart:
if stock[key] > 0:
total += prices[key]
return total
【问题讨论】:
-
我希望
compute_bill在遍历购物车中的所有项目之后返回总数,而不是仅在第一个之后。也就是说,你应该突出return total。
标签: python dictionary