简体   繁体   English

python 中的逻辑门

[英]Logic gates in python

I need feedback on how to improve this code and I am also a bit confused on how to do the XNOR gate, some advice would be really useful.我需要有关如何改进此代码的反馈,并且我对如何执行 XNOR 门也有些困惑,一些建议会非常有用。 I have tested the code myself and i found that my while loops are wrong, I am not very good with using while gates and would greatly appreciate it if someone could help.我自己测试了代码,发现我的 while 循环是错误的,我不太擅长使用 while 门,如果有人能提供帮助,我将不胜感激。

user_input1 = int(input("please enter a one digit binary:"))
user_input2 = int(input("please enter another one digit binary:"))
gate_selection = str(input("please select which gate you want(enter gate in uppercase):"))
while user_input1 or user_input2 != "1" or "0":
    print("please enter a valid binary digit")
    gate_selection = str(input("please select which gate you want(enter gate in uppercase):"))
    while user_input1 or user_input2 != "XOR" or "NOR" or "NAND" or "NOT" or "AND" or "OR":
        print("please enter a valid logic gate")
        user_input1 = int(input("please enter a one digit binary: "))
        user_input2 = int(input("please enter another one digit binary: "))
    
#NOT gate
if gate_selection == "NOT":
    if user_input1 == "1":
        print("0")
    elif user_input2 == "0":
        print("1")

#OR gate
elif gate_selection == "OR":
    if user_input1 and user_input2 == "1":
        print("1")
    elif user_input1 or user_input2 == "0":
        print("1")
    elif user_input1 and user_input2 == "0":
        print("0")

#AND gate
elif gate_selection == "AND":
    print(user_input1 * user_input2)

#NAND gate
elif gate_selection == "NAND":
    user_input1 * user_input2 = NAND_output1
    if NAND_output1 == "1":
        print("0")
    elif NAND_output1 == "0":
        print("1")

#NOR gate
elif gate_selection == "NOR":
    if user_input1 and user_input2 == "1":
        print("0")
    elif user_input1 or user_input2 == "0":
        print("0")
    elif user_input1 and user_input2 == "0":
        print("1")

#XOR gate
elif gate_selection == "XOR":
    if user_input1 == user_input2:
        print("0")
    elif user_input1 != user_input2:
        print("1")

There were many mistakes in the code.代码中有很多错误。 Here is the final code after correcting all of them.这是纠正所有这些后的最终代码。 I have added the code for XNOR gate too:我也添加了 XNOR 门的代码:

while True:
    gate_selection = str(input("please select which gate you want(enter gate in uppercase):"))
    if gate_selection in ["XOR","NOR","NAND","NOT","AND" ,"OR","XNOR"]:
        try:
            user_input1 = int(input("please enter a one digit binary:"))
            user_input2 = int(input("please enter a one digit binary:"))
            if user_input1 in [0,1] and user_input2 in [0,1]:
                break
            raise ValueError
        except ValueError:
            print("please enter a valid binary digit")
    else:
        print("please enter a valid logic gate")

#NOT gate
if gate_selection == "NOT":
    if user_input1:
        print(0)
    else:
        print(1)

#OR gate
elif gate_selection == "OR":
    if user_input1 or user_input2:
        print(1)
    else:
        print(0)

#AND gate
elif gate_selection == "AND":
    print(user_input1 * user_input2)

#NAND gate
elif gate_selection == "NAND":
    if user_input1 and user_input2:
        print(0)
    else:
        print(1)

#NOR gate
elif gate_selection == "NOR":
    if user_input1 or user_input2:
        print(0)
    else:
        print(1)

#XOR gate
elif gate_selection == "XOR":
    if user_input1 == user_input2:
        print(0)
    else:
        print(1)

#XNOR gate
elif gate_selection == "XNOR":
    if user_input1 == user_input2:
        print(1)
    else:
        print(0)

Here are some simple logic gates in Python 3.以下是 Python 3 中的一些简单逻辑门。

gate_selection = "OR"
A = 1
B = 0

if gate_selection == "NOT":
    print(int(not(A)))

elif gate_selection == "OR":
    print(int(A | B))

elif gate_selection == "NOR":
    print(int(not(A | B)))

elif gate_selection == "AND":
    print(int(A & B))

elif gate_selection == "NAND":
    print(int(not(A & B)))

elif gate_selection == "XOR":
    print(int(A ^ B))

elif gate_selection == "XNOR":
    print(int(not(A ^ B)))

This way is very simple and increases readability.这种方式非常简单并且增加了可读性。 In Python, the |在 Python 中,| operator can be used for bitwise OR, & is for bitwise AND, and ^ is for bitwise XOR.运算符可用于按位或,& 用于按位与,^ 用于按位异或。 Combining these with a simple bitwise not() produces the 7 logic gates.将这些与简单的按位 not() 结合起来产生 7 个逻辑门。 Documentation can be found here文档可以在这里找到

An XNOR gate following this format could look like this.遵循这种格式的 XNOR 门可能如下所示。 Note that python interprets the integer values 1 and 0 as boolean Truth values.请注意,python 将 integer 值 1 和 0 解释为 boolean 真值。 So you can treat them as such.所以你可以这样对待它们。

user_input1 = int(user_input1)
user_input2 = int(user_input2)

if gate_selection == 'XNOR':

    if user_input1 == user_input2:

        print(1)

    else:

        print(0)

As @Countour - Integral pointed out some of your logic seems to be off.正如@Countour - Integral 指出的那样,您的某些逻辑似乎不正确。

In python writing the statement if variable evaluates the variables Truth value against None .在 python 中编写语句if variable根据None评估变量的真值。 If the variable isn't None it returns True .如果变量不是None它返回True

elif gate_selection == "OR":

    # This logic states that if user_input1 is True and user_input2 is equivalent to the string "1" then it should continue. 
    if user_input1 and user_input2 == "1":
        print("1")

    # This logic states that if user_input1 is True or user_input2 is equivalent to the string "0" then it should continue.     
    elif user_input1 or user_input2 == "0":
        print("1")
    elif user_input1 and user_input2 == "0":
        print("0")

A much simpler way of doing this logic gate would be to cast your inputs to integers and evaluate their truth values directly执行此逻辑门的一种更简单的方法是将输入转换为整数并直接评估它们的真值

user_input1 = int(user_input1)
user_input2 = int(user_input2)

elif gate_selection == "OR":

    if user_input1 or user_input2:
        
        print(1)

    else:
        
        print(0)

First of all in the while loop it should be like this: while (user_input1:= 1 and user_input1) or (user_input2 != 1 and user_input2):首先在while循环中应该是这样的:while (user_input1:= 1 and user_input1) or (user_input2 != 1 and user_input2):

In python a string is considered to be true which is why doing: "NOR" or "NAND" or "NOT" or "AND" or "OR" will always return true.在 python 中,字符串被认为是真的,这就是为什么这样做:“NOR”或“NAND”或“NOT”或“AND”或“OR”将始终返回真。 you can create a list of operations and check if the entered string is in the list.您可以创建一个操作列表并检查输入的字符串是否在列表中。 valid_operations = ["XOR", "NOR", "NAND", "NOT", "AND", "OR"] while gate_selection not in valid operations: print("please enter a valid logic gate") gate_selection = str(input("please select which gate you want(enter gate in uppercase):")) valid_operations = ["XOR", "NOR", "NAND", "NOT", "AND", "OR"] 而 gate_selection 不在有效操作中: print("请输入一个有效的逻辑门") gate_selection = str(input ("请 select 你想要哪个门(输入大写门):"))

XNOR gate: returns true if user_input1 == user_input2 else returns false XNOR 门:如果 user_input1 == user_input2 返回 true,否则返回 false

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

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