我能够定义第一组门输入并从相应的门产生输出。 我正在尝试使用前一个门的输出并在我的新门中使用它们。
例如,我有一个设置为0的输入,称为PREV,一个名为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
答案 0 :(得分:1)
您的代码存在许多问题。函数内定义的变量仅存在于该函数内部,其他函数无法看到它们。因此,您需要更改功能的交互方式。
你们没有一个函数返回他们计算的内容,因此他们将返回None
。因此,您需要在函数中放置return
语句,当您调用函数时,您需要保存它返回的内容,以便将其传递给其他函数,&amp; /或打印。
此外,代码中的远重复过多。不要定义一系列全部执行相同操作的功能。这首先打破了定义函数的主要目的。创建一个执行给定任务的函数,每次需要执行该任务时,使用该函数。
这里有一些代码可以帮助您入门。它没有读取任何用户输入,我会让你处理这项工作。相反,它使用标准product
模块中的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))
<强>输出强>
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