Python布尔电路?

Python布尔电路?,python,boolean,discrete-mathematics,truthtable,Python,Boolean,Discrete Mathematics,Truthtable,我正在尝试创建模拟布尔电路的代码,并根据布尔逻辑通过布尔电路返回true或false(一或零)。我将在下面张贴电路图。然而,我得到的结果是: Ran 9 tests in 0.141s OK 电路1: 电路2: 代码如下: #!/usr/bin/python import unittest # An example: exercise 12.3.1 def circuit_3_1(x, y): a1 = x + y a2 = not y # note: the en

我正在尝试创建模拟布尔电路的代码,并根据布尔逻辑通过布尔电路返回true或false(一或零)。我将在下面张贴电路图。然而,我得到的结果是:

Ran 9 tests in 0.141s
OK
电路1: 电路2: 代码如下:

#!/usr/bin/python
import unittest

# An example: exercise 12.3.1
def circuit_3_1(x, y):
    a1 = x + y
    a2 = not y

    # note: the end result should be returned as 0/1
    #   - bool() transforms any sum larger than 0 into True (False otherwise)
    #   - int() transforms any True into 1 (0 otherwise)
    # int( bool ( n )) will therefore transforms any sum larger than 0 into 1 (0 otherwise)
    return int( bool(a1 and a2))

def circuit_3_2(x, y):
    a1 = not x
    a2 = not y
    a3 = (not a1) and (not a2)


    return int( bool((a3)))
4
def circuit_3_4(x, y, z):
    a1 = (not((not x) and y and z))
    a2=((not x) or y or(not z))
    return int( bool(a1 and a2))


class circuits_unit_tests( unittest.TestCase ):

    def test_circuit_3_1(self):
        self.assertEqual( circuit_3_1(0,0), 0)
        self.assertEqual( circuit_3_1(0,1), 0)
        self.assertEqual( circuit_3_1(1,0), 1)
        self.assertEqual( circuit_3_1(1,1), 0)

    def test_circuit_3_4_000(self):
        self.assertEqual( circuit_3_4(0,0,0), 1)

    def test_circuit_3_4_001(self):
        self.assertEqual( circuit_3_4(0,0,1), 1)

    def test_circuit_3_4_010(self):
        self.assertEqual( circuit_3_4(0,1,0), 1)

    def test_circuit_3_4_011(self):
        self.assertEqual( circuit_3_4(0,1,1), 0)

    def test_circuit_3_4_100(self):
        self.assertEqual( circuit_3_4(1,0,0), 1)

    def test_circuit_3_4_101(self):
        self.assertEqual( circuit_3_4(1,0,1), 0)

    def test_circuit_3_4_110(self):
        self.assertEqual( circuit_3_4(1,1,0), 1)

    def test_circuit_3_4_111(self):
        self.assertEqual( circuit_3_4(1,1,1), 1)

def main():
        unittest.main()

if __name__ == '__main__':
        main()

如果所有测试都通过,则表示输出与您期望的一致。您已经编写了单元测试代码,所以它所做的一切都是检查您的方法的输出是否符合您的预期。您是在试图让测试失败还是什么?这里似乎没有问题。我从未使用过单元测试仪。我想我的教授想让我写通过测试的代码。所以我认为我写的方法有正确的逻辑?所以你发布了一些代码,其中单元测试通过了。你的问题是什么?如果所有的测试都通过了,这意味着输出与你期望的一样。您已经编写了单元测试代码,所以它所做的一切都是检查您的方法的输出是否符合您的预期。您是在试图让测试失败还是什么?这里似乎没有问题。我从未使用过单元测试仪。我想我的教授想让我写通过测试的代码。所以我认为我写的方法有正确的逻辑?所以你发布了一些代码,其中单元测试通过了。你的问题是什么?