Python - 陷入codecademy培训

时间:2017-05-23 06:48:15

标签: python

坚持使用codecamdemy - 请帮忙

从他们的例子开始:

def sum(numbers):
    total = 0
    for number in numbers:
        total += number
    return total
n = [1, 2, 5, 10, 13]
print sum(n)

和一些以前创建的listts / dictionaries:

shopping_list = ["banana", "orange", "apple"]
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

他们要求: 定义一个函数compute_bill,它将一个参数food作为输入。 在函数中,创建一个初始值为零的变量total。 对于食物清单中的每个项目,将该项目的价格添加到总计。 最后,返回总数。

我的代码无效:

# Write your code below!
def compute_bill(food):
    total = 0
    for x in food:
        total += x #or total+= int(x) or anything else I might think of
        return total

来自codecademy的注意事项: 忽略您要结算的商品是否有货。 请注意,您的功能应适用于任何食物清单。 非常感谢你的帮助!

0 个答案:

没有答案