所以,我试图将这个 bool 逻辑表放入 python 中,以便为我们提供 x(逻辑表末尾)http://imgur.com/Yarq75h 的输出。
到目前为止我的代码是:
#Main instructions
A = input('Enter 0 or 1 for 1st input: ')
B = input('Enter 0 or 1 for 2nd input: ')
C = input ('Enter 0 or 1 for 3rd input: ')
print 'The logic diagram, LOGIC-1 evaluates for the input values, A, B and C to X'
print "input 1 =",A
print "input 2 =",B
print "input 3 =",C
print (A and not B) and (not B or C)
但是,输出与我的输入不一致。例如,输入 0,0,0 给出 0。但我希望它为 1。此外,输入 1,1,1 给出 false。
最佳答案
您的代码看起来没问题。我认为您在手动评估逻辑时犯了一些错误。以下是解决方法:
- 以“
A B C
”开头 - 重写为“
A N C
”,其中N
列为“NOT B
” - 计算“
A AND N
”(使用'&
'表示AND
) - 计算“
N OR C
”(使用'|
'表示OR
) - 最后,计算最终输出值。
像这样:
A B C A N C A&N N|C (A&N)&(N|C)
0 0 0 0 1 0 0 1 0
0 0 1 0 1 1 0 1 0
0 1 0 0 0 0 0 0 0
0 1 1 0 0 1 0 1 0
1 0 0 1 1 0 1 1 1
1 0 1 1 1 1 1 1 1
1 1 0 1 0 0 0 0 0
1 1 1 1 0 1 0 1 0
当我用 Python 运行一个循环来检查它时,我得到的答案与我在这里手工得到的答案相同。
关于Python bool 逻辑链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550894/