簡體   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