【问题标题】:PyCharm returns incorrect Boolean value when comparing two "and" expressions [duplicate]比较两个“和”表达式时,PyCharm 返回不正确的布尔值 [重复]
【发布时间】:2021-12-24 12:04:18
【问题描述】:

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

(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)

【问题讨论】:

  • 你能分享代码吗?当我尝试时,我得到的结果与你的不同
  • 多么奇怪,当我复制并粘贴您的代码时,我得到“一切都很好!”但是当我使用打印功能时,我仍然得到相同的错误值。是否与打印功能有关?
  • 欢迎来到 Stack Overflow。请编辑问题并包含您尝试过的代码,"Minimal, Reproducible, Example."
  • print(4
  • @rv.kvetch 非常感谢!感谢您抽出宝贵时间帮助我解决这个问题!

标签: python pycharm boolean


【解决方案1】:

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

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

您还提到了一个特定的表达式(4 &lt; 5) and (9 &lt; 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!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2014-04-30
    • 2014-12-08
    • 2017-02-05
    相关资源
    最近更新 更多