python - 比较两个“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)
解决方案
结果似乎与我的预期一样,在 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!
推荐阅读
- sql - 根据 hive 中的列值获取每种可能性的总和 - 聚合表
- c++ - 在 C++ 中计算任何结构的简单方法
- typescript - react-select 中 menuList 样式键的状态类型是什么?
- r - 将非对称矩阵转换为方阵
- c - 在 Makefile 中使用外部依赖项
- python - choice() 接受 2 个位置参数,但给出了 3 个
- java - Kotlin 中选定文本的自定义操作问题
- hibernate - 从 JBoss AS 7.1 迁移到 JBoss EAP 7.3.0
- r - 每列具有不同值的查找表
- ocaml - Return a list of the odd indices using List.map