逻辑门实现失败

2024-04-25 07:52:37 发布

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

我已经做了一个逻辑门,它接收两个输入,然后在这种情况下,这些输入将传入一个与门。例如,用户将输入0和0,然后与门将处理为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是无法识别的,所以有人知道我做错了什么吗?你知道吗

这就是问题所在:从here到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 

Tags: andthepath程序内容inputdef语句
1条回答
网友
1楼 · 发布于 2024-04-25 07:52:37

我为您清理了代码:您应该阅读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);

相关问题 更多 >