Python-卡在我的培训中

Python-卡在我的培训中,python,Python,坚持使用codecamdemy-请帮助 从他们的例子开始: def sum(numbers): total = 0 for number in numbers: total += number return total n = [1, 2, 5, 10, 13] print sum(n) 以及一些以前创建的列表/字典: shopping_list = ["banana", "orange", "apple"] stock = { "banana"

坚持使用codecamdemy-请帮助

从他们的例子开始:

def sum(numbers):
    total = 0
    for number in numbers:
        total += number
    return total
n = [1, 2, 5, 10, 13]
print sum(n)
以及一些以前创建的列表/字典:

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作为输入。 在函数中,创建初始值为零的变量总计。 对于食物列表中的每一项,将该项的价格添加到总价中。 最后,返回总数

我的代码不起作用:

# 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中的注释: 忽略您正在计费的项目是否有库存。 请注意,您的函数应该适用于任何食物列表。
非常感谢你的帮助

什么是食物?它是一些字典吗?请定义“不工作”-到底发生了什么(错误消息、预期和接收的输入、调试时看到了什么…)是食物字典?您正在循环中返回总计。尝试在循环外返回。food只是我收集的一些随机函数属性,在循环外返回:“哦,再试一次。compute_bill(['apple'])导致类型错误:不支持+=:'int'和'str'的操作数类型”