【发布时间】:2021-12-05 23:29:21
【问题描述】:
我正在处理两个用户定义的函数,一个调用第一个函数,它确定给定的输入是平行四边形还是矩形,但是当我设置我的 if 语句时,即使输入满足“真”类别后一个函数,它仍然打印为“False”(即使我用下面的东西替换“False”打印,比如打印“No”)我认为这是我在后一个语句中调用前一个函数的方式。
#the function, isPara below works perfect
def isPara(s1, s2):
'''if base lengths are same, it will return true'''
if b1 == b2:
isPara = True
print 'True'
else:
isPara = False
print 'False'
#however when I call isPara into isRec, the output displays as false even if it's true or doesn't #print false
def isRec(s1, s2, angle):
'''if isPara is true '''
if isPara is True:
if angle == 90:
isRec = True
print 'True'
else:
isRec = False
print 'Not true'
s1 =3
s2 = 3
angle = 90
isPara (s1, s2)
isRec( s1, s2, angle)
【问题讨论】:
-
isPara是一个函数对象,不会和True是同一个对象。您可能打算调用该函数,例如if isPara(s1, s2):你也没有从isPara返回任何东西,所以isPara将总是返回None。你想写return True,而不是isPara = True。 -
用您自己的话说,您认为
if isPara is True的作用究竟是什么?为什么?用您自己的话来说,您如何调用函数?你想在那个时候调用一个函数吗?你的代码这样做吗? (提示:之前你写isPara (s1, s2)时,那是调用函数吗?)
标签: python function python-2.7 boolean