Python为逻辑含义返回错误的真值表

时间:2016-09-21 11:30:41

标签: python boolean-logic discrete-mathematics

enter image description here

我在Python中实现了上述含义,但它没有返回预期的结果:

  True       True None
  True      False None
 False       True True
 False      False None

我的python代码是:

def implies(a,b):
    if a:
        return b
    else:True
    return
for p in (True, False):
    for q in (True, False):
        print("%10s %10s %s" %(p,q,implies((p or q) and (not p), q)))

我不明白这里的矛盾。没有暗示假不是吗?为什么不按照它应该打印True?

1 个答案:

答案 0 :(得分:2)

def implies(a,b):
    if a:
        return b
    else:True
    return

您的错误位于最后两行,如果!a,您没有返回特定值,因此结果为None。 你想要:

def implies(a,b):
    if a:
        return b
    else:
        return True
相关问题