【问题标题】:Stuck at Learnpython.org Tutorial (Regarding Functions)卡在 Learnpython.org 教程(关于函数)
【发布时间】:2012-08-08 03:20:28
【问题描述】:

几个小时前我刚刚开始学习 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!

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

【问题讨论】:

    标签: python function python-3.x


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,他们都是很好的答案,我很震惊人们在这里回复的速度如此之快! :)
    【解决方案2】:

    我想你想做的是:

    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
    

    【讨论】:

      【解决方案3】:

      我刚刚陷入同样的​​问题。我的 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()
      

      【讨论】:

        【解决方案4】:

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

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

        您还需要在此处对问题中的代码进行格式化,以便于破译。

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

        【讨论】:

          【解决方案5】:

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

          最后...我意识到您并不真的需要所有功能的意大利面(就像他们设置的那样。-我试图使这些功能按照我认为的需要工作;但它是不是。所以我很惊讶我能跳到下一章。

          我学到了什么?..区分 returnprint 并使用它。

          说得好。这是代码!:

          # 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)
          

          【讨论】:

            【解决方案6】:

            我是初学者,这是我的有效答案

            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(benefit):

            return benefit + " is a benefit of functions!"
            
            pass
            

            定义名称_the_benefits_of_functions():

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

            name_the_benefits_of_functions()

            【讨论】:

              【解决方案7】:

              回答(简短而甜蜜):

                  # 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()
              

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-02-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-09
                • 1970-01-01
                • 2011-03-06
                相关资源
                最近更新 更多