Python代码帮助!代码学院不是很有帮助

时间:2016-02-02 09:49:40

标签: python python-2.7

couchbase git repository elasticsearch plugin

 shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        total += item
    return total

以上是我的代码,我知道它需要找到值,但即使我尝试food.values()它也会出错。我怎样才能做到这一点?

由于

1 个答案:

答案 0 :(得分:1)

你应该把你的代码作为代码/文本,而不是图片! 无论如何,只是一个没有任何检查/验证的非常原始的实现将是这样的:

def computer_bill(food):
    total = 0.0
    for i in food:
        if i in prices:
            total += float(prices[i])
    return total


stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

print "-------"
lfood = ["banana","orange"]
print "total is:", computer_bill(lfood)

输出:

总计是:5.5