2014-12-04 50 views
0

我的程序中有兩個布爾變量,分別叫做after和late。只是爲了確保每個變量有正確的值,我測試了每一個與下面的命令:爲什麼表達式是真實的和真實的? (python 2.7)

print(after) 
print(late) 

該程序打印

false 
true 

預期。然而,當我運行下面的代碼:

if after and late: 
    print('true and true') 
elif after and (not late): 
    print('true and false') 
elif (not after) and late: 
    print('false and true') 
elif (not after) and (not late): 
    print('false and false') 

程序打印

'true and true' 

這意味着表達後,晚正在評估爲true。爲什麼這種評估是真實的,即使真假都會產生錯誤?

+1

''後''和'後'的字符串,也許? – nneonneo 2014-12-04 04:45:50

+0

更多的代碼片段將會有所幫助 – Codeek 2014-12-04 04:47:46

+1

它們絕對不是布爾值。檢查案件。 Python的布爾值是大寫字母。 – dyoo 2014-12-04 04:51:03

回答

4
>>> print(True and False) 
False 

Boolean值必須在開始一個大寫字母。我想你使用字符串。你可以用type(after)來檢查。

你不需要手動劃分所有的情況來「調試」你的程序。這不是Python的目的... 只需print評估代碼print(after+' and '+late),使用type像我以上或使用您的交互式python控制檯玩耍。

+0

謝謝。我只是測試它,它們是字符串。 – SarcasticSully 2014-12-04 04:51:18

相關問題