这个代码应该在检查库存中是否有可用的物品(库存变量)之后计算所列物品总价格,我想知道为什么我不打印任何东西。
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(food):
total=0
for i in food:
if stock[i]>0:
total+=prices[i]
return total
print total
compute_bill(shopping_list)
答案 0 :(得分:1)
您的打印需要在退货声明之前:
def compute_bill(food):
total=0
for i in food:
if stock[i]>0:
total+=prices[i]
print total
return total
答案 1 :(得分:0)
如果在IDE中运行,则需要使用print
print compute_bill(shopping_list)
返回语句后print total
为unreachable
。
所以要么忘记print
,要么放在print
之前 return