几个小时前我刚刚开始学习 Python,似乎有一个我根本无法解决的问题。
他们要求我:
添加一个名为 list_benefits()- 的函数,它返回以下字符串列表:“更有条理的代码”、“更易读的代码”、“更容易的代码重用”、“允许程序员共享和连接代码”
添加一个名为 build_sentence(info) 的函数,它接收一个包含字符串的参数并返回一个以给定字符串开头并以字符串结尾的句子“是函数的好处!”
运行并查看所有功能协同工作!
我已经用谷歌搜索了这个问题,但所有这些似乎都是针对以前版本的 python,我希望有一种更新的方法来做到这一点。
给定代码:
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print build_sentence(benefit)
name_the_benefits_of_functions()
预期输出:
More organized code is a benefit of functions!
More readable code is a benefit of functions!
Easier code reuse is a benefit of functions!
Allowing programmers to share and connect code together is a benefit of functions!
我试过的:
def list_benefits():
benefits_list = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
return benefits_list
def build_sentence(benefit):
return "%s is a benefit of functions!" % list_benefits()
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print(build_sentence(benefit))
name_the_benefits_of_functions()
输出:
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
谁能告诉我我做错了什么?