为什么我得到假在这个布尔问题

  • 本文关键字:布尔 问题 python boolean
  • 更新时间 :
  • 英文 :

A= True
B= True
y = not(A or B) == (not(A)) and (not(B))
print (y)

在python上输出为False。

不应该是真的(假==假)吗?

你需要注意操作的顺序。

一般情况下,在有歧义的情况下,最好使用括号。

在您的例子中,它被翻译为

A = True
B = True
y = (not(A or B) == (not(A))) and (not(B))

如果我们仔细看,它是:

y = (not(True or True) == (not(True))) and (not(True))
y = (not(True) == (False)) and (False)
y = (False == False) and (False)
y = True and False
y = False

希望这对你有帮助,祝你旅途顺利

你需要这样做来得到你想要的:y = not(A or B) == (not(A) and not(B))

in what you did you got not(A or B) == (not(A))为真,之后你有正确,而不是(B),这是错误的!

最新更新