【发布时间】:2016-06-29 09:55:20
【问题描述】:
我正在学习 python,但我的程序有一个简单的问题。我有两个字典,其中一个键作为字符串,值连接到它们。可以说这是一家有水果和价格的商店。
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
# Function to calculate the bill
def compute_bill(food):
total = 0
for number in food:
if (stock[number]>0):
total += prices[number]
stock[number] -= 1
return total
print compute_bill(shopping_list)
如果水果有库存,则将价格添加到账单中并减少库存数量。如果没有,请不要做任何事情。
错误信息:
使用包含 1 个苹果、1 个梨和 1 个香蕉的列表调用 compute_bill 得到 0 而不是正确的 7
我不知道为什么这段代码不能正常工作。
【问题讨论】:
-
从那里得到回报。您希望所有项目在函数终止之前运行。
-
所以还是不行吗?
-
@Ev.Kounis 现在工作。一条不必要的线。 :)
标签: python dictionary