这些 if 语句背后的逻辑是什么?

What is the logic behind these if statements?

我是编码新手,所以我不太了解这些东西背后的逻辑。我确实理解基本的 if 语句。有人可以请 eli5 下面的逻辑以及为什么它不起作用吗?

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("mode", help="Use encode/decode")
args = parser.parse_args()
mode = args.mode
encode = 'encode'
decode = 'decode'

现在是我的 if 语句。这个工作正常,但我想要相反的。

if mode == encode or mode == decode:
   print("Mode is supported!")

这个很好用,经过一番搜索后发现

if not (mode == encode or mode == decode):
   print("Mode isn't supported")
   exit()

为什么下一个不是第一个的倒数?

if mode != encode or mode != decode:
   print("Mode is encode or decode)
   exit()

编辑并回答 很多人告诉我去看看德摩根定律,我照做了,我花了很长时间才真正理解(直到我在 vs code 中做了一些测试功能,我才完全理解)。我想我现在想通了

    if mode == encode or mode == decode:
       print("Mode isn't encode or decode")
mode = encode True or false = True
mode = decode False or True = True
mode = test False or False = False

所以下一个是不正确的。

    if mode != encode or mode != decode:
       print("Mode isn't encode or decode")
       exit()
mode = encode False or True = True
mode = decode False or True = True
mode = test True or True = False

下一个正确。

     if not (mode == encode or mode == decode):
       print("Mode isn't encode or decode")
       exit()
mode = encode (True or False) = True # not(True) = False
mode = decode (False or True) = True # not(True) = False
mode = test (False or False) = False # not(False) = True

if not (mode == encode or mode == decode):
似乎是
的真正反面 if mode == encode or mode == decode:

根据我的新知识,我相信下一个应该有效。

    if mode != encode and mode != decode:
       print("Mode isn't encode or decode")
       exit()
mode = encode False and True = False
mode = decode True and False = False
mode = test True and True = True

我知道我对自己的回答有点啰嗦,但我希望我能以一种能够帮助其他代码新手的方式来阐明这一点。

基本上,如果我们有一个变量是您想要设置的模式,即 mode_to_be_used ,并且这个变量包含一个 string 要么是 "decode" 要么是 "encode", 你可以测试这个变量是否包含数据 "encode" 通过做:

mode_to_be_used = "encode"
if mode_to_be_used == "encode" :
  print("The mode here was : ", mode_to_be_used )

只有当mode_to_be_used的值为"encode"

时才会打印模式

所以,这样做:

mode_to_be_used = "decode"
if mode_to_be_used == "encode" :
  print("The mode here was : ", mode_to_be_used )

不会在屏幕上打印任何内容。

通过扩展,做:

mode_to_be_used = "encode"
if mode_to_set  == "encode" or mode_to_set  == "encode" :
  print("The mode here was : ", mode_to_be_used )

将打印 mode_to_be_used 中的值,如果它包含 "encode" "decode"。但是如果它包含 "foo" 则不会。

要解决这个问题,您可以这样做:

mode_to_be_used = "test"

if mode_to_set  == "encode" or mode_to_set  == "encode" :
  print("The mode here was : ", mode_to_be_used )
  if mode_to_set  == "encode":
    print("now doing the encoding")
  if mode_to_set  == "decode":
    print("now doing the decoding")

else :
  print("This value is not expected for mode_to_set :", mode_to_set  )

如果您是Python的新手,我建议您这样做: 想一想您将遇到的所有可能情况:

1) mode = "encode"
2) mode = "decode"
3) mode = "yolol" (something that not in both cases, this can represent all cases that not equal to encode or decode)

对于第一个: mode == encode or mode == decode: 情况一和情况二都会通过,因为模式是二;案例三不通过 对于第二个: not (mode == encode or mode == decode) 案例一和案例二都不会通过,而案例三会,因为你基本上只是翻转第一个场景 对于第三个: if mode != encode or mode != decode:。现在这三个都将通过,因为当 mode != decode 时 encode 为 True(也意味着 encode != decode,这是 True),decode != mode 也为 True,yolo != True。 您可能想查看德摩根定律以了解有关布尔表达式的更多信息

根据德摩根定律 'not(A or B)' is equals to 'not(A) and not(B)'

通过将此应用于您的表达式,'not (mode == encode or mode == decode)' 等于 'not(mode == encode) and not(mode == decode)' 等于 'mode != encode and mode != decode'