Python 如何使用函数将异或门的输出带到与门中?
我能够定义第一组门输入,并根据门产生输出。 我正在尝试使用前一个gate的输出,并在我的新gate集合中使用它们 例如,我有一个名为PREV的输入设置为0,还有一个名为XOR的变量,它从第一个XOR门获取输出,以便可以重用 程序代码:Python 如何使用函数将异或门的输出带到与门中?,python,python-3.6,Python,Python 3.6,我能够定义第一组门输入,并根据门产生输出。 我正在尝试使用前一个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 inpu
#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
你的代码有很多问题。函数中定义的变量只存在于该函数中,其他函数无法看到它们。因此,您需要更改函数的交互方式 所有函数都不会返回它们计算的值,因此它们不会返回任何值。因此,您需要在函数中放入return语句,当您调用函数时,您需要保存它返回的内容,以便将其传递给其他函数和/或打印 而且,代码中有太多的重复。不要定义一组执行相同操作的函数。这首先违背了定义函数的主要目的。创建一个执行给定任务的函数,每次需要执行该任务时,使用该函数 这里有一些代码可以帮助您开始。它不读取任何用户输入,我会让你处理这项工作。相反,它使用标准itertools模块的乘积函数创建位模式,以馈送到逻辑门
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))
输出
我相信你可以用一个小得多的例子来说明你的问题。这个问题中有太多不相关的代码。我让feeing Chepneril只是过滤不相关的内容,发布相关内容对程序进行了更改,并将上面的示例缩小了Y很抱歉,如果你们不得不看到这么多,我将其裁剪为将一个门转移到另一个门,然后是解决和MK2门的部分请你把完整的回溯贴出来作为你问题的编辑?哦,你的意思是你想要一份完整的程序代码而不是精简版本?天哪,你刚刚摧毁了我的抱负XD,但那是一部惊人的史诗,与我的相比,代码维护得很好。我明白为什么这里的人都是专业人士。但是,难道没有一种可能的方法可以修复我的代码版本,使其能够工作吗?或者它只是维护得很差啊,我明白了,你想让我研究这个例子,然后将它应用到我自己的代码中,以便我解决这个问题。这正是我计划要做的。感谢yall提供的帮助,我观察到一个问题导致另一个问题,正如我用python探索的那样。在我用视觉编程之前basic@PL8YR好吧,你可以尝试修复你的代码,但是扔掉它,从头开始可能更容易;但是,是的,我写这篇文章是为了向您展示如何编写返回结果的函数,以及如何将这些结果传递到其他函数中。顺便说一句,在Python中,函数和简单变量通常使用小写名称。所有大写的名称用于常量。这不是一条硬性规定,但它是标准样式,如果你采用这种样式,其他人会更容易阅读你的代码。这是一个有趣的故事,如果我放弃代码XD,PM现在就太晚了。所以我最好理解你的示例,与我的同事一起解决这个问题,因为9天后我将不得不提交我的最终程序,因为这是我课程的一部分,但我会听取您关于维护代码的建议,特别是与AND和XOR门相关的功能,因为我意识到我实际上可以减少其中的代码数量
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