【发布时间】:2019-04-09 13:01:44
【问题描述】:
下面是我试图用来运行函数的 python 代码。
def list_benefits():
return "More organized code", "More readable code", "Easier code
`reuse", "Allowing programmers to share and connect code together"`
def build_sentence(benefit):
return "%s is a benefit of functions!" %benefit
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefi in list_of_benefits:
print(build_sentence(benefi))
name_the_benefits_of_functions()
我不明白为什么我们需要有变量“list_of_benefits”,为什么不能在最后一个函数中直接使用“list_benefits”。上面的代码运行良好,但如果我从任何地方删除“list_of_benefits”,我会收到以下错误 -
TypeError: 'function' 对象不可迭代
【问题讨论】:
-
您必须使用
list_benefits()而不是list_benefits,注意括号,这就是您调用函数的方式,否则,您只是在引用函数对象本身。 -
请澄清您的问题。您可以直接使用 list_benefits() 作为 for 循环目标。
标签: python python-3.x function typeerror