Python bool 逻辑链

标签 python

所以,我试图将这个 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/

相关文章:

c++ - 在 C++ 中读取 python UUID 字节字符串?

python - 如何仅使用 pcolor/pcolormesh 绘制网格线

python - Softbank Pepper 机器人与 Naoqi - 使用 Python 进行摄像头直播延迟

python - 尝试为非字符串复制 Python 的字符串实习功能

python - 我无法制作 1x1 矩形

python - 如何打印在 tensorflow 代码中完成的计算的执行时间?

python - 扩展已在 Django 中扩展的模板

python - 具有标称或有序轴类型的 Bokeh 图

python - Networkx 图不具有其值(value)

python - zlib 使用 python 压缩/解压缩文件