Logic gates in python [closed]

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. I have tested the code myself and i found that my while loops are wrong, can someone help point out where I have gone wrong.

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

Solution 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:

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)

Solution 2:

Here are some simple logic gates in 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 | 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. Documentation can be found here

Solution 3:

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

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

XNOR gate: returns true if user_input1 == user_input2 else returns false