逻辑门实现失败

时间:2017-11-11 11:31:41

标签: python logic

我已经制作了一个逻辑门,它接收两个输入,然后在这种情况下输入到AND门。例如,用户将输入0和0,然后AND门将处理为0.

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

file.hasNext()

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

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

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

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

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")

1 个答案:

答案 0 :(得分:0)

我为你清理了你的代码:你应该阅读python语法,f.e。 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);