2014-12-04 111 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

相关问题