如何使用函数将异或门的输出带入与门?

2024-04-26 05:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够定义第一组门输入,并根据门产生输出。 我正在尝试使用前一个gate的输出,并在新的gate集合中使用它们。你知道吗

例如,我有一个名为PREV的输入设置为0,还有一个名为XOR的变量,它从第一个XOR门获取输出,以便可以重用。你知道吗


程序代码:

   #we will be solving the second AND/XOR gates.
   def AND_MK2(XOR,PREV):
   #we define inputs of first XOR gate and PREV; new inputs enter new 
   gates.
       if XOR == 0 and PREV == 0:
           AND_5(XOR,PREV) #again, based on the condition we call 
           different functions.
           print("Now solve the XOR gate otherwise move on")
           gate_path_B ()
       elif XOR == 1 and PREV == 0:
           AND_6(XOR,PREV)
           print("Now solve the XOR gate otherwise move on")
           gate_path_B ()
       else:
           print("Error")

   #since the inputs can only be 00 or 10; PREV is = 0
   def AND_5(XOR,PREV): #called into the main program
       print(XOR , " AND " , PREV , " = 0")
       AND_II = 0 #stores output in variable for later use

   def AND_6(XOR,PREV):
       print(XOR , " AND " , PREV , " = 0")
       AND_II = 0

   #Program starts here by defining the inputs
   A = None
   while A is None:
       try:
           A = int(input("Enter a number 1 or 0: "))
       except ValueError:
           print("You didn't enter either number")
   print(str(A));

   B = None
   while B is None:
       try:
           B = int(input("Enter a number 1 or 0: "))
   except ValueError:
       print("You didn't enter either number")
  print(str(B));

def gate_path_A (): #type any of the three numbers given for their option
    print("Press 3 for MOVE")
    x = int(input("write 1/2/3: "))

    if x == 3:
        gate_path_B()
    else:
        print("error")

def gate_path_B (): #same fundamentals to gate path A but different 
options given
    print("Press 1 for AND_MK2")
    print("Press 3 for MOVE")
    y = int(input("write 1/2/3: "))

    if y == 1:
        AND_MK2(XOR,PREV)
    elif y == 2:
        XOR_MK2(XOR,PREV)
    elif y == 3:
        gate_path_C()
    else:
        print("error")

gate_path_A()
gate_path_B()

PREV = None
while PREV is None:
    try:   
        PREV = 0 #No actual inputs take place
    except ValueError:
        print("Inputs only 0; we have not input in the first place")
print(str(PREV));

XOR = None #we will define XOR based on the output of its first gate.
while XOR is None:
    try:
        XOR(XOR,PREV)
    except ValueError:
        print("Incorrect")
print(str(XOR));

#defines our two logic gates with the given inputs
first_AND (A,B)
first_XOR (A,B)
AND_MK2(XOR,PREV);
XOR_MK2(XOR,PREV);

我只是新的整个调试过程中发现哪里是错误存在,但主要的问题是我得到一个错误

error : gate_path_B is not defined in my AND_MK2 or the XOR_MK2.

如果您需要有关程序执行和产生的结果的其他信息,请在评论中提及。你知道吗

执行阶段产生的错误

Traceback (most recent call last):
  File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
  systems\Coursework\Python Cicruits\Test MK5.py", line 169, in <module>
  gate_path_A()
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
   systems\Coursework\Python Cicruits\Test MK5.py", line 146, in 
   gate_path_A
first_AND (A,B)
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
systems\Coursework\Python Cicruits\Test MK5.py", line 7, in first_AND
gate_path_A()
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
systems\Coursework\Python Cicruits\Test MK5.py", line 148, in gate_path_A
first_XOR (A,B)
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
systems\Coursework\Python Cicruits\Test MK5.py", line 43, in first_XOR
gate_path_A()
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
systems\Coursework\Python Cicruits\Test MK5.py", line 150, in gate_path_A
gate_path_B()
File "C:\Users\waliu\Documents\Waliur Uni stuff\Information 
systems\Coursework\Python Cicruits\Test MK5.py", line 161, in gate_path_B
AND_MK2(XOR,PREV)
NameError: name 'XOR' is not defined

Tags: andthepathinnoneisusersfile
1条回答
网友
1楼 · 发布于 2024-04-26 05:08:14

你的代码有很多问题。函数中定义的变量只存在于该函数中,其他函数看不到它们。所以你需要改变你的函数之间的交互方式。你知道吗

没有函数返回它们计算的值,因此它们将返回None。因此,您需要在函数中放入return语句,当您调用函数时,您需要保存它返回的内容,以便它可以传递给其他函数,&;/。你知道吗

而且,代码中有太多的重复。不要定义一堆执行相同操作的函数。这违背了最初定义函数的主要目的。创建一个执行给定任务的函数,每次需要执行该任务时,使用该函数。你知道吗

这里有一些代码让你开始。它不读取任何用户输入,我会让你处理那项工作。相反,它使用标准itertools模块中的product函数来创建位模式,以提供给逻辑门。你知道吗

from itertools import product

def and_gate(a, b):
    return a & b

def xor_gate(a, b):
    return a ^ b

def half_adder(a, b):
    sum_bit = xor_gate(a, b)
    carry_bit = and_gate(a, b)
    return carry_bit, sum_bit 

def full_adder(a, b, c):
    c0, s0 = half_adder(a, b)
    c1, s = half_adder(c, s0)
    c = xor_gate(c0, c1)
    return c, s

# Test

bits = (0, 1)
print('Half-adder test')
for a, b in product(bits, repeat=2):
    c, s = half_adder(a, b)
    print('{} + {} = {} {}'.format(a, b, c, s))

print('\nFull-adder test')
for c, a, b in product(bits, repeat=3):
    c1, s = full_adder(a, b, c)
    print('{} + {} + {} = {} {}'.format(c, a, b, c1, s))

输出

Half-adder test
0 + 0 = 0 0
0 + 1 = 0 1
1 + 0 = 0 1
1 + 1 = 1 0

Full-adder test
0 + 0 + 0 = 0 0
0 + 0 + 1 = 0 1
0 + 1 + 0 = 0 1
0 + 1 + 1 = 1 0
1 + 0 + 0 = 0 1
1 + 0 + 1 = 1 0
1 + 1 + 0 = 1 0
1 + 1 + 1 = 1 1

相关问题 更多 >

    热门问题