我正在研究兩個用戶定義的函式,一個呼叫第一個函式,確定給定的輸入是平行四邊形還是矩形,但是當我設定 if 陳述句時,即使輸入滿足后一個函式的“真”類別,它仍然列印為“假”(即使我用下面的東西替換“假”列印,比如列印“否”)我認為這是我在后一條陳述句中呼叫前一個函式的方式。
#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)
uj5u.com熱心網友回復:
正如 Brian 所說,您需要從函式中回傳值,您還需要將 isPara 傳遞給函式 isRec。
def isPara(b1, b2):
'''if base lengths are same, it will return true'''
return True if b1 == b2 else False
def isRec(angle, isPara):
'''if isPara is true '''
return True if isPara is True and angle == 90 else False
s1, s2, angle = 3, 3, 90
isPara = isPara(s1, s2)
print(isRec(angle, isPara))
isPara 提供了 2 個變數,你在函式內部都不使用這兩個變數,你將變數更改為 b1 和 b2。如果您將變數傳遞給函式并希望在函式中更改它的名稱,請使用
def isPara(b1, b2):
此外,由于 isRec 既不使用 s1 也不使用 s2,您不需要將這些值傳遞給 isRec 函式。
此腳本的輸出:
True
uj5u.com熱心網友回復:
isPara()是一個帶有 2 個引數的函式,因此您需要從isRec(). 您的代碼中幾乎沒有更新,它按預期作業:
def isPara(s1, s2):
'''if base lengths are same, it will return true'''
isPara = False
if s1 == s2:
isPara = True
print ('True')
else:
isPara = False
print ('False')
return isPara
def isRec(s1, s2, angle):
'''if isPara is true '''
if isPara(s1, s2): <<< Here is the change, call function
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)
輸出:
True
True
True
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327757.html
標籤:Python 功能 python-2.7 布尔值
