几小时前我刚刚开始学习Python,似乎有一个我似乎无法获得的问题。
他们让我:
添加一个名为list_benefits()的函数 - 它返回以下字符串列表:“更有组织的代码”,“更易读的代码”,“更轻松的代码重用”,“允许程序员共享和连接代码”
添加一个名为build_sentence(info)的函数,它接收一个包含字符串的参数,并返回一个以给定字符串开头并以字符串“是函数的好处!”结尾的句子。
运行并查看所有功能一起工作!
我已经用Google搜索了这个问题,但所有这些似乎都是针对以前版本的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!
有谁能告诉我我做错了什么?
答案 0 :(得分:2)
每次调用build_sentence()
函数时,您只希望它使用您在benefit
参数中指定的单个权益来构建句子。
def build_sentence(benefit):
return "%s is a benefit of functions!" % benefit
对于此循环的每次迭代:
for benefit in list_of_benefits:
print(build_sentence(benefit))
将一个好处传递给build_sentence()
函数,这就是您要打印的内容。
答案 1 :(得分:1)
我认为你想要做的是:
def build_sentence(benefit):
return "%s is a benefit of functions!" % benefit
您在name_the_benefits_of_functions
到list_benefits()
的通话将结果列表存储在本地变量list_of_benefits
中。现在你迭代(正确),但在你的build_sentence
函数中,你反复获得一个新的好处列表。而不是这样做,只需添加传入的单个benefit
。
我知道你是Python新手,所以欢迎。我相信你会看到generators
上的部分,但是这里有一个修改过的例子,使用它来获得乐趣。
def list_benefits():
benefits_list = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
i = 0
while i < len(benefits_list):
yield benefits_list[i]
i += 1
答案 2 :(得分:1)
我只是遇到了同样的问题。我的name_the_benefits_of_functions()
函数有点垃圾,因为它只重复了四次,而不是在list_of_benefits
耗尽时优雅地失败,但这对我有用:
# Modify this function to return a list of strings as defined above
def list_benefits(count): #just a list of benefits. It spits out whatever number 'count' happens to be
list_of_benefits = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
return list_of_benefits[count]
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit): #tacks on the sentence end, and that's it
return "%s is a benefit of functions!" % benefit
def name_the_benefits_of_functions():
count = 0
while count < 4: # not very graceful, but oh well
benefit = list_benefits(count)
print build_sentence(benefit)
count += 1
name_the_benefits_of_functions()
答案 3 :(得分:0)
当您应该依次传递列表中的每个字符串时,您将字符串列表传递给函数build_sentence。
list_of_benefits = list_benefits()
for item in list_of_benefits:
print build_sentence(item)
您还需要在此处的问题中格式化代码,以便更容易解密。
我希望我能正确理解你的问题。
答案 4 :(得分:0)
这对我有用。花了我一段时间,我得到了缩略词,整个错误的伽玛和所有类型的烟花,但正确的答案。
最后......我意识到你并不真正需要所有功能的意大利面(就像他们设定的那样。 - 我试图让这些功能起作用,因为我认为它是必需的;但它是不是。所以我很惊讶我弹出了下一章。
我学过的课程?..区分 return 和 print 并使用它。
说得好。这是代码!:
# Modify this function to return a list of strings as defined above
s= ("More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together")
def list_benefits(s):
for b in s:
return b
def build_sentences(s):
for b in s:
print b + " is a benefit of functions!"
list_benefits(s)
build_sentences(s)
答案 5 :(得分:0)
我是初学者,这是我的答案
def list_benefits():
benefit_list= ["More organized code","More readable code", "Easier code reuse","Allowing programmers to share and connect code together"]
return benefit_list
pass
def build_sentence(好处):
return benefit + " is a benefit of functions!"
pass
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()
答案 6 :(得分:0)
答案(简短而甜蜜):
# Modify this function to return a list of strings as defined above
def list_benefits():
return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentence(benefit):
return "%s is a benefit of functions!" % benefit
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()
删除你的清单,只需返回字符串。