支持Python中的Logic Gate Simulation程序

时间:2016-01-20 15:46:37

标签: function boolean logic

我正在尝试在python中创建逻辑门仿真程序,以便用户可以选择他们想要模拟的逻辑门的类型。一旦选择,它们就可以输入输入,程序应该将所选逻辑门的输出值返回给用户。

这是我到目前为止所做的:

split

当我运行程序并输入AND时,输入1和1它仍然返回0并且我无法理解为什么。

4 个答案:

答案 0 :(得分:2)

这是我的代码版本(我不得不为AS Level做作业,这对你有用!):

print("Logic Gate Calculator")


def AND(a, b):  # AND Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 1:  # AND Gate logic
        return 1
    else:
        return 0


def NAND(a, b):  # NAND Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 1:  # NAND Gate logic
        return 0
    elif a == 1 and b == 0:
        return 0
    elif a == 0 and b == 1:
        return 0
    else:
        return 1


def OR(a, b):  # OR Gate
    a = int(a)
    b = int(b)
    if a == 1:  # OR Gate Logic
        return 1
    elif b == 1:
        return 1
    else:
        return 0


def NOR(a, b):  # NOR Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 0:  # NOR Gate Logic
        return 1
    elif a == 0 and b == 1:
        return 1
    elif a == 0 and b == 0:
        return 1
    else:
        return 0


def XOR(a, b):  # XOR Gate
    a = int(a)
    b = int(b)
    if a == 1 and b == 0:  # XOR Gate Logic
        return 1
    elif a == 1 and b == 1:
        return 1
    else:
        return 0


def main():  # The main program
    run = True
    while run:  # While loop
        question = input("What type of gate do you want to use OR, AND, NOR, or NAND or (Q)uit")  # Logic Gate chooser
        if question == "AND" or question == "and" or question == "And":  # If the user selects AND
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = AND(a, b)  # Calling the AND Function
            print("The output will be:", x)  # Output result
        elif question == "OR" or question == "or" or question == "Or":  # If the user selects OR
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = OR(a, b)  # Calling the OR Function
            print("The output will be:", x)  # Output result
        elif question == "NOR" or question == "nor" or question == "Nor":  # If the user selects NOR
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = NOR(a, b)  # Calling the NOR function
            print("The output will be:", x)  # Output result
        elif question == "NAND" or question == "nand" or question == "Nand":  # If the user selects NAND
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = NAND(a, b)  # Calling the NAND function
            print("The output will be:", x)  # Output result
        elif question == "XOR" or question == "xor" or question == "Xor":  # If the user selects XOR
            a = input("Enter value for input 1 (1 or 0):")  # Getting the Logic Gate's 1st input
            b = input("Enter value for input 2 (1 or 0):")  # Getting the Logic Gate's 2nd input
            x = XOR(a, b)  # Calling the XOR function
            print("The output will be:", x)  # Output result
        elif question == "Q" or question == "q":  # Quiting the program
            run = False
        else:
            print("Please enter one of the shown logic gates")  # Error handling


main()

答案 1 :(得分:1)

这有效:

def AND (a,b):
    a=int(a)
    b=int(b)
    if a == 1 and b == 1:
        return 1
    else:
        return 0

你必须告诉python a和b是整数 - (有比这个方法更好的方法)

答案 2 :(得分:0)

一个=真 B =真

输出= a和b 打印(输出)

一个=真 B =假

输出= a和b 打印(输出)

请试试这个

答案 3 :(得分:-1)

有效。使用 如果a == 1: 如果b == 1: 返回1 其他 返回0