【发布时间】: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 将值传递给其他函数。我想要的只是解释执行过程中所遵循的步骤。
【问题讨论】:
-
en.wikipedia.org/wiki/Boolean_algebra#Basic_operations 在您的代码中检查运算符
and
标签: python python-2.7 printing return