麻烦理解函数返回learnpython.org的“函数”部分

时间:2016-09-19 21:11:35

标签: python

我正在学习learnpython.org上的“函数”练习并且有一个问题,为什么我的代码版本在输出的每个字符串后返回“None”(我首先定义字符串列表然后返回)它在特定的函数中)而不是解决方案(它们在一行中返回字符串列表)。以下是他们给我的代码,说明以及我们期望看到的内容:

说明:

  1. 添加一个名为list_benefits()的函数,它返回以下字符串列表:“更有组织的代码”,“更易读的代码”,“更轻松的代码重用”,“允许程序员共享和连接代码”< / p>

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

  3. 运行并查看所有功能一起工作!

  4. 代码(给定):

    # Modify this function to return a list of strings as defined above
    def list_benefits():
        pass
    
    # Modify this function to concatenate to each benefit - " is a benefit of functions!"
    def build_sentence(benefit):
        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()
    

    预期输出:

    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!
    

    现在,这是我的解决方案的实现 - 一切似乎都没问题,除了我每隔一行得到一个“无”:

    代码(我的):

    # Modify this function to return a list of strings as defined above
    def list_benefits():
        list = "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"
        return list
    
    
    # Modify this function to concatenate to each benefit - " is a benefit of functions!"
    def build_sentence(benefit):
        print "%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()
    

    输出(我的):

    More organized code is a benefit of functions!
    None
    More readable code is a benefit of functions!
    None
    Easier code reuse is a benefit of functions!
    None
    Allowing programmers to share and connect code together is a benefit of functions!
    None
    

    这就是实际的解决方案,它产生了前面显示的预期输出:

    代码(解决方案):

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

    正如您所看到的,我的代码与解决方案之间的唯一区别在于,我将字符串分配给变量“list”,然后返回该变量,与解决方案代码一样,它们返回字符串本身的列表。我的版本每隔一行返回“无”的原因究竟是什么,但正确的版本不是这样?

    谢谢!

1 个答案:

答案 0 :(得分:0)

区别在于您的build_sentence返回值,而不是您的list_benefits返回值。

该行:

 print build_sentence(benefit)

指示Python解释器打印build_sentence函数的输出 - 但是如果你查看build_sentence实现,你会发现你没有明确地return该函数的任何输出。所以它返回None,这就是打印的内容。

相反,您的build_sentence承担print结果本身的工作(解决方案代码的build_sentence不执行此操作)。您已经创建了一个函数示例,该函数具有所谓的“副作用” - 函数对系统(或用户)状态的影响,而不仅仅是函数输出的传递。在这种情况下,副作用是您的代码产生的控制台输出看起来接近正确的原因,并且误导您认为build_sentence根据规范工作并将注意力集中在{{{ 1}}。

list_benefits本身没有问题,只是它使用list_benefits作为变量名,掩盖了Python基本类型之一的名称。这是允许的,但这是一个坏习惯,迟早可能会导致代码可维护性问题。通常,人们在试图调用变量list时经常使用名称lst