【问题标题】:stuck on print and return卡在打印和返回
【发布时间】:2016-01-08 21:37:44
【问题描述】:
def isItEven2(n):

    if (n%2 == 0):
      print('true it is even')
    else:
      print('false it is not even')

def isInCenter2(n):

      if (n>100):
        print('true it is center')
      else:
        print('false it is not center')

def seatLocation2(n):

    if isItEven2(n) and not isInCenter2(n):
          print 'check1'
          return "Right"
    elif not isItEven2(n) and not isInCenter2(n):
          print 'check2'
          return "Left"
    else:
      return "Center"

def seatLocation3(n):

    if not isInCenter2(n) and isItEven2(n):
        print 'check1'
        return "Right"
    elif not isInCenter2(n) and not isItEven2(n):
        print 'check2'
        return "Left"
    else:
        return "Center"

seatLocation2(n)seatLocation3(n) 的行为不同,谁能告诉我为什么seatLocation2(n) 和seatLocation3(n) 的输出不同?真的花了好几个小时才弄明白。

下面是示例输出

调用seatLocation2(2)时的输出:

true it is even
true it is even
false it is not center
check2

并在调用seatLocation3(2)时输出:

false it is not center    
true it is even 
false it is not center
true it is even
check2
Out[1043]: 'Left'

我知道我使用了 print,所以我得到了 None 值,所以最好使用 return 将值传递给其他函数。我想要的只是解释执行过程中所遵循的步骤。

【问题讨论】:

标签: python python-2.7 printing return


【解决方案1】:

您需要从 isItEven2isInCenter2 函数返回一些内容 (True/False)。

def isItEven2(n):
    if (n%2 == 0):
      print('true it is even')
      return True
    else:
      print('false it is not even')
      return False
def isInCenter2(n):
  if (n>100):
    print('true it is center')
    return True
  else:
    print('false it is not center')
    return False

【讨论】:

  • 我知道我使用了 print 所以我得到 None 值所以最好使用 return 将值传递给其他函数。我想要的只是执行过程中遵循的步骤
  • @learner 您实际上是如何调用这些函数的? n 是什么?
  • 我正在使用 seatLocation2(2) 或 seatLocation2(101) 调用类似的东西
【解决方案2】:

理解这两种seatLocation方法的执行差异的关键在于理解两个概念:

  • 具有 return 语句的方法没有将返回None,在条件中评估为False
  • 尽可能设置短路条件。也就是说,如果您有condition1 and condition2,而condition1 是否定的,则永远不会费心找出condition2 是什么,因为最终结果将始终是否定的

好的,在您的代码中,函数将总是返回None(或者可以说是False),所以当单步执行seatLocation2(2)时,我们会得到:

  • 检查if isItEven2(2) and not isInCenter(2)
    • 它调用isItEven2(2) 打印“true it is even”
    • 但它返回 None 是假的,所以它打破了 if
  • 检查elif not isItEven2(2) and not isInCenter2(2)
    • 它调用not isItEven2(2) 打印“true it is even”
    • 但它返回None这是错误的,但它被否定了(not)所以它仍然有可能成为True
    • 它调用not isInCenter2(2) 打印“false it is not center”
    • 但它返回 None 为假,但将 (not) 否定为真
  • 所以我们进入if
    • 打印“check2”(并返回样本输出中遗漏的“Left”)

另一方面,如果我们关注seatLocation3(2),它会变成这样:

  • 检查if not isInCenter2(2) and isItEven2(2)
    • 调用not isInCenter2(2) 打印“false it is not in center”
    • 但返回的None被否定了,所以我们必须继续测试
    • 调用isItEven2(2) 打印“true it is even”
    • 但返回 `None" 为假,我们跳出 if
  • 检查elif not isInCenter2(2) and not isItEven2(2)
    • 调用not isInCenter2(2) 打印“false it is not in center”
    • 但返回 None 被否定,所以我们必须继续测试
    • 调用not isItEven2(2) 打印“true it is even”
    • 但返回“None”为假,取反为真,所以整体表达式再次为真
  • 所以我们再次进入 if 块
    • 并打印“check2”,然后返回“Left”

因此它们的行为实际上是相同的,这意味着您的两个函数都返回 None,但由于您在左侧或右侧执行 not 不同,因此它们似乎行为不端。但是当你有 False and ... 这样的东西时,做布尔短路,第二部分永远不会被执行。

【讨论】:

  • 哇,这就是冠军提出解决方案的方式。非常感谢你。你让我的一天变得清晰。只是有点基本的疑问 isItEven2(2) 打印“true it is even”,而 not isItEven2(2) 也打印“true it is even”但从基本我知道 not True 等于 False 。 (所以不会 isItEven2(2) 导致 false 吗?)
  • @learner,这是您问题的核心:您必须将输出与实际返回值分开。你可以打印任何东西,但是当你返回None,然后not None 变成真的!
【解决方案3】:

当您从函数返回 None 时,它​​将被解释为 False。在您的情况下,无论“n”的值如何,函数“seatlocation2(n)”和“seatlocation3(n)”的第二个条件都将被执行,因为您不同时使用“isItEven”和“isItCenter”。这应该足以解释代码流。

代码流将精确地遵循以下步骤:

  1. 函数seatlocation2(n)被调用

  2. 它在 if 条件中调用 isItEven() 函数,结果为 False(因为没有),因此条件不会被执行。

  3. 它进入 elif 块,在该块中它采用 isItEven() 的“not”,将“False”返回值转换为“True”;然后它调用返回 False 的 isInCenter();其中的“不”为真。两者的“和”都是真的。这会导致第二个条件始终执行,因为您的函数始终返回 None。

所有其他条件在当前逻辑流程中都已过时

【讨论】:

  • 如果我在 seatLoaction 函数中得到 None ,为什么我会得到与其他函数相关联的结果(因为我没有在那些函数中使用 return)
  • 正如我所说,第二个条件将始终为真,因此将打印 check2。我希望这很清楚。现在,当您从条件调用这些函数(inItEven、isInCenter)时,它们会被调用并执行其中的指令(打印语句)。这就是为什么你会看到他们的输出
  • 你得到了两次打印'true it is even',因为函数'isItEven()'被调用了两次。首先在 if 条件中,然后在 elif 中。整个 'isItEven()' 执行,因此所有这些打印。现在你可能会问为什么 isItCenter() 函数没有被调用两次。这是因为在 if 条件中,'isItEven()' 返回 False,因此它不会检查 'and' 的下一部分(因为即使一个为假,and 也是假的)。因此它在第二个条件下只执行一次
  • 是的,一切都清楚,但如果我尝试执行 isItEven2(102) 和 isInCenter2(102) 输出是“真的它是偶数”,而这两种情况都是如此,但有点怀疑。为什么会发生这种情况为什么“真的是偶数”和“真的是中心”导致整理后一个。
  • 了解与“和”相关的语句的行为。当 'and' 左边的语句为 False 时,右边的条件不会被执行。您可以在对“seatlocation2(n)”函数的调用中看到这一点。座位位置3(n) 展示了图片的另一面。因为左边的条件是“真”(由于不是无),你会从“isItEven”和“isInCenter”得到打印。由于它们在两个单独的条件(if 和 elif)中被调用,因此它们中的每一个在第二个条件变为真之前出现两次并停止执行进一步的条件。希望一切顺利
猜你喜欢
  • 2020-07-20
  • 1970-01-01
  • 2017-08-21
  • 2016-05-01
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
相关资源
最近更新 更多