Python代码帮助!代码学院没用

2024-04-26 01:39:47 发布

您现在位置:Python中文网/ 问答频道 /正文

Code and instructions found here

 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

上面是我的代码,我知道它需要找到值,但即使我尝试食品.价值观()它给出了一个错误。我怎样才能做到这一点?你知道吗

谢谢


Tags: andappleherefoodstockcodeitemlist
1条回答
网友
1楼 · 发布于 2024-04-26 01:39:47

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

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

相关问题 更多 >

    热门问题