【问题标题】:Trouble understanding function returns in "Functions" section of learnpython.org在 learnpython.org 的“函数”部分中无法理解函数返回
【发布时间】:2016-09-20 05:50:35
【问题描述】:

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

说明:

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

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

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

代码(给定):

# 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”,然后返回该变量,与解决方案代码一样,它们正在返回字符串列表本身。我的版本每隔一行返回“无”,而正确的版本却没有,究竟是什么原因?

谢谢!

【问题讨论】:

    标签: python


    【解决方案1】:

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

    行:

     print build_sentence(benefit)
    

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

    相反,您的build_sentence 承担了print 处理结果本身的工作(解决方案代码的build_sentence 没有这样做)。您创建了一个函数示例,该函数具有所谓的“副作用”——函数对系统(或用户)状态的影响,而不仅仅是函数输出的传递。在这种情况下,副作用是您的代码生成的控制台输出看起来几乎正确的原因,这会误导您认为build_sentence 正在按照规范工作并将您的注意力集中在list_benefits 上。

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

    【讨论】:

    • 我实际上完全忽略了解决方案是在 build_sentence() 中使用“return”而不是“print”,所以我什至从未考虑过这可能是我的实现中的问题。谢谢,这很有意义!
    • 如果这个答案是您问题的解决方案,您可以将其标记为这样,在其分数下方打上绿色勾号。
    猜你喜欢
    • 2012-11-21
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2012-06-21
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多