Python:这个函数出了什么问题?

时间:2014-09-25 23:22:33

标签: python python-2.7

这个代码应该在检查库存中是否有可用的物品(库存变量)之后计算所列物品总价格,我想知道为什么我不打印任何东西。

    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)

2 个答案:

答案 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 totalunreachable

所以要么忘记print,要么放在print 之前 return