首页 > 解决方案 > 比较两个“and”表达式时,PyCharm 返回不正确的布尔值

问题描述

我对 Python 还是很陌生,这让我很困惑。我有三台计算机都运行 PyCharm,它们都返回相同的错误布尔值。这是问题所在。我一直在我的 PC 和 MacBook Pro 上使用 PyCharm,所以我不认为这是软件故障。我一直在研究自动化无聊的东西,它要求输入这些布尔表达式:

(4 < 5) and (5 < 6)= 真

(4 < 5) and (9 < 6)= False(由于某种原因返回“True”)

(1 == 2) or (2 == 2)= 真

基本上只是展示 and, or, not 运算符的功能。

表达式(4 < 5) and (9 < 6)每次都返回 True(在 PyCharm 中)。但是,我可以打开 Python,输入完全相同的表达式,它会返回为 False,这也是应该的。有没有其他人经历过这个?我会做错什么吗?

实际代码:

print( 4 < 5 ) and (5 < 6)

print( 4 < 5) and (9 < 6)

print(1 == 2) or (2 == 2)

标签: pythonpycharmboolean

解决方案


结果似乎与我的预期一样,在 Windows 上使用 Pycharm,但在这种情况下这无关紧要。

assert (4 < 5) and (5 < 6)
assert (4 < 5) and (9 < 6) is False
assert (1 == 2) or (2 == 2)

您还提到了一个特定的表达式(4 < 5) and (9 < 6)在 Pycharm 中给您带来了麻烦。也许尝试运行下面的代码?它似乎在 Pycharm 中使用 Run 命令按预期工作 - 尽管我通常只是为此目的使用键盘快捷键。

if (4 < 5) and (9 < 6):
    print('Something went wrong! Please uninstall Pycharm :-)')
else:
    print('All good on the home front!')

输出:

All good on the home front!

推荐阅读