繁体   English   中英

逻辑门实施失败

[英]Logic Gate implementation fails

我制作了一个逻辑门,该逻辑门接收两个输入,然后在这种情况下将其馈入与门。 例如,用户将输入0和0,然后AND门将处理为0。

这里的问题是,当我们使用IF语句确定两个输入时,无法识别它们,否则程序中用于处理两个输入的所有其他内容以及用于输出的临时存储。

A = input("Enter a value of 1 or 0: ")
B = input("Enter a value of 1 or 0: ")
print(A)
print(B)

因此,上面的部分我能够输入输入并为其创建存储。 该程序告诉我A和B无法识别,所以有人知道我在这里做错了吗?

这就是问题发生的地方:从这里到else语句的所有内容都将被忽略。

def first_AND ():
if A == 0 and B == 0: 
    AND_1()
    print(AND)
    print("Now solve the XOR gate")
    gate_path_A()
elif A == 1 and B == 0:
    AND_2()
    print(AND)
    print("Now solve the XOR gate")
    gate_path_A()
elif A == 0 and B == 1:
    AND_3()
    print(AND)
    print("Now solve the XOR gate")
    gate_path_A()
elif A == 1 and B == 1:
    AND_4()
    print(AND)
    print("Now solve the XOR gate")
    gate_path_A()
else: 
    print("Error")

它会跳过我所有的elif语句,只显示错误。

def AND_1():
print(A & " AND " & B & " = 0")
AND = 0

def AND_2():
print(A & " AND " & B & " = 0")
AND = 0

def AND_3():
print(A & " AND " & B & " = 0")
AND = 0

def AND_4():
print(A & " AND " & B & " = 1")
AND = 1 

我为您整理了代码:您应该阅读python语法,例如https://docs.python.org/2/reference/index.html (适用于2.7.x)并做一些教程

# define global 
AND = None

#define the used functions
def gate_path_A():
    print("Reached gate_path_A()")
    return


def first_AND (A,B):
    if A == 0 and B == 0: 
        AND_1(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 1 and B == 0:
        AND_2(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 0 and B == 1:
        AND_3(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 1 and B == 1:
        AND_4(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    else: 
        print("Error")
    return  


def AND_1(A,B):
    print(A , " AND " , B , " = 0")
    AND = 0
    return

def AND_2(A,B):
    print(A , " AND " , B ," = 0")
    AND = 0
    return

def AND_3(A,B):
    print(A , " AND " , B , " = 0")
    AND = 0
    return

def AND_4(A,B):
    print(A , " AND " , B , " = 1")
    AND = 1
    return

主程序

# get one integer from user
a = None
while a is None:
    try:
        a = int(input("Enter a number: "))
    except ValueError:
        print("Not an integer value...")
print(str(a));

# get second integer from user 
# you should really put this in a def and return the integer: DRY principle
b = None
while b is None:
    try:
        b = int(input("Enter a number: "))
    except ValueError:
        print("Not an integer value...")
print(str(b));

# call to the first and thing and supply the params your got from user
first_AND(a,b);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM