为什么表达式“假”和“真”的计算结果是“真”?(python 2.7)
我的程序中有两个布尔变量,分别是after和late。为了确保每个变量都得到正确的值,我使用以下命令测试了每个变量:为什么表达式“假”和“真”的计算结果是“真”?(python 2.7),python,Python,我的程序中有两个布尔变量,分别是after和late。为了确保每个变量都得到正确的值,我使用以下命令测试了每个变量: print(after) print(late) 程序打印 false true 'true and true' 正如所料。但是,当我运行以下代码时: if after and late: print('true and true') elif after and (not late): print('true and false') elif (not after
print(after)
print(late)
程序打印
false
true
'true and 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')
程序打印
false
true
'true and true'
这意味着表达式after和late的计算结果为true。为什么即使真的和假的结果应该是假的,这种评估也会是真的
>>> print(True and False)
False
值的开头有一个大写字母。我猜你用的是字符串。您可以使用type(after)
进行检查
您不需要手动划分所有案例来“调试”您的程序。这不是Python的初衷。。。
只需print
打印已评估的代码print(在+'和'+之后)
,像上面我一样使用键入
,或者使用交互式python控制台进行游戏
值的开头有一个大写字母。我猜你用的是字符串。您可以使用type(after)
进行检查
您不需要手动划分所有案例来“调试”您的程序。这不是Python的初衷。。。
只需
print
打印已计算的代码print(在+'和'+late之后)
,使用上面我所说的键入
,或者使用交互式python控制台来玩。可能是after
和late
字符串吗?再加上一小段代码就可以了,它们绝对不是布尔型的。检查一下箱子。Python的布尔值是大写的。如果答案满足,请打印(type(after),type(later)),然后将其标记并关闭。可能是after
和late
字符串吗?一个小片段会很有帮助,它们肯定不是布尔值。检查一下箱子。Python的布尔值是大写的。如果答案满足,请打印(键入(后),键入(后)),然后将其关闭。谢谢。我刚测试过,它们是字符串。谢谢。我刚测试过,它们是字符串。