0

几个小时前我刚刚开始学习 Python,似乎有一个我根本无法解决的问题。

他们要求我:

  1. 添加一个名为 list_benefits()- 的函数,它返回以下字符串列表:“更有条理的代码”、“更易读的代码”、“更容易的代码重用”、“允许程序员共享和连接代码”

  2. 添加一个名为 build_sentence(info) 的函数,它接收一个包含字符串的参数并返回一个以给定字符串开头并以字符串结尾的句子“是函数的好处!”

  3. 运行并查看所有功能协同工作!

我已经用谷歌搜索了这个问题,但所有这些似乎都是针对以前版本的 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!

谁能告诉我我做错了什么?

4

7 回答 7

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()函数,这就是您要打印的内容。

于 2012-08-08T03:26:12.043 回答
1

我想你想做的是:

def build_sentence(benefit):
    return "%s is a benefit of functions!" % benefit

您的调用是name_the_benefits_of_functionslist_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
于 2012-08-08T03:26:07.747 回答
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()
于 2012-11-15T01:09:10.920 回答
0

当您应该将列表中的每个字符串依次传递给函数时,您正在将字符串列表传递给函数 build_sentence。

list_of_benefits = list_benefits()
for item in list_of_benefits:
    print build_sentence(item)

您还需要在此处格式化您的代码问题,以便更容易破译。

我希望我正确理解了你的问题。

于 2012-08-08T03:29:35.080 回答
0

这对我有用。我花了一段时间才得到首字母缩略词,错误的全部伽玛和所有类型的烟花,但正确的答案。

最后......我意识到你并不真的需要所有功能的意大利面(就像他们设置的那样。-我试图让这些功能按照我认为的需要工作;但事实并非如此。所以我很惊讶我得到了弹出来移动到下一章。

我学到的教训?..区分返回打印并使用它。

说得好。这是代码!:

# 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)
于 2013-01-14T17:21:46.947 回答
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()

于 2013-02-27T21:20:49.397 回答
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()

摆脱您的列表,只需返回字符串。

于 2013-09-19T17:59:53.603 回答