简体   繁体   中英

Python returns wrong truth table for logical implication

在此处输入图片说明

I have implemented the above implication in Python but it does not return the expected results:

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

My python code is:

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

I don't understand the contradiction here. None implies False doesn't it? And why not print True like it should?

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

Your error is in the last two lines, if !a, you aren't returning a specific value, so the result is None . You want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM