Логический код Python: И, NAND, ИЛИ, NOR, XOR, XNOR, и НЕ симуляция - PullRequest
0 голосов
/ 08 июня 2018

Я пытаюсь выяснить, почему мой код не работает.Почему некоторые из моих логических элементов, например OR, не дают правильного вывода?Возьмите, к примеру, ворота OR.Когда я запускаю код и передаю 1 в качестве значения для A и B, вывод все равно False.Я попытался настроить его, и он все еще дает мне False в качестве вывода.

Ниже приведен пример того, что я сделал до сих пор:

aInput = int(input('Enter value for A: '))
bInput = int(input('Enter value for B: '))

#AND Gate
if aInput == 1 and bInput == 1:
    ANDGate = "True"
    ANDGateNum = 1
else:
    ANDGate = "False"
    ANDGateNum = 0

print('AND Gate output is', ANDGate, 'or', ANDGateNum)

#NAND Gate
if aInput == 1 and bInput == 1:
    NANDGate = "False"
    NANDGateNum = 0
else:
    NANDGate = "True"
    NANDGateNum = 1

print('NAND Gate output is', NANDGate, 'or', NANDGateNum)

#OR Gate
if aInput == 1 and bInput == 1:
    ORGate = "True"
    ORGateNum = 1
if aInput == 1 and bInput == 0:
    ORGate = "True"
    ORGateNum = 1
if aInput == 0 and bInput == 1:
    ORGate = "True"
    ORGateNum = 1
else:
    ORGate = "False"
    ORGateNum = 0

print('OR Gate output is', ORGate, 'or', ORGateNum)

#NOR Gate
if aInput == 1 and bInput == 1:
    NORGate = "False"
    NORGateNum = 0
if aInput == 1 and bInput == 0:
    NORGate = "False"
    NORGateNum = 0
if aInput == 0 and bInput == 1:
    NORGate = "False"
    NORGateNum = 0
else:
    NORGate = "True"
    NORGateNum = 1

print('NOR Gate output is', NORGate, 'or', NORGateNum)

#XNOR Gate
if aInput == 1 and bInput == 1:
    XNORGate = "True"
    XNORGateNum = 1
if aInput == 1 and bInput == 0:
    XNORGate = "False"
    XNORGateNum = 0
if aInput == 0 and bInput == 1:
    XNORGate = "False"
    XNORGateNum = 0
else:
    XNORGate = "True"
    XNORGateNum = 1

print('XNOR Gate output is', XNORGate, 'or', XNORGateNum)

#XOR Gate
if aInput == 1 and bInput == 1:
    XORGate = "False"
    XORGateNum = 0
if aInput == 1 and bInput == 0:
    XORGate = "True"
    XORGateNum = 1
if aInput == 0 and bInput == 1:
    XORGate = "True"
    XORGateNum = 1
else:
    XORGate = "False"
    XORGateNum = 0

print('XOR Gate output is', XORGate, 'or', XORGateNum)

#NOT Gate
if aInput == 1: 
    NOTGate = "False"
    NOTGateNum = 0

else:
    NOTGate = "True"
    NOTGateNum = 1

print('NOT Gate output is', NOTGate, 'or', NOTGateNum)

Я также попытался заменить and между aInput и bInput с or, который, кажется, работает, но немного трудно повторить использование с воротами XOR и XNOR:

#OR Gate
if aInput == 1 or bInput == 1:
    ORGate = "True"
    ORGateNum = 1
else:
    ORGate = "False"
    ORGateNum = 0

print('OR Gate output is', ORGate, 'or', ORGateNum)

#NOR Gate
if aInput == 1 or bInput == 1:
    NORGate = "False"
    NORGateNum = 0
else:
    NORGate = "True"
    NORGateNum = 1

print('NOR Gate output is', NORGate, 'or', NORGateNum)

Ответы [ 2 ]

0 голосов
/ 08 июня 2018

Вас также могут интересовать побитовые операторы для AND (&), OR (|) и XOR (^):

a = int(input('Enter value for A (0 or 1): '))
b = int(input('Enter value for B (0 or 1): '))

print("AND: ", bool(a & b))
print("OR:  ", bool(a | b))
print("XOR: ", bool(a ^ b))

Это будет выводить только то, что вы ожидаете, если пользователь вводит либо0 или 1.

Enter value for A (0 or 1): 1
Enter value for B (0 or 1): 1
AND:  True
OR:   True
XOR:  False

Enter value for A (0 or 1): 1
Enter value for B (0 or 1): 0
AND:  False
OR:   True
XOR:  True
0 голосов
/ 08 июня 2018

Как обсуждено в комментариях, попробуйте использовать elif.Добро пожаловать в переполнение стека!

#OR Gate  
if aInput == 1 and bInput == 1:  
    ORGate = "True"  
    ORGateNum = 1  
elif aInput == 1 and bInput == 0:  
    ORGate = "True"  
    ORGateNum = 1  
elif aInput == 0 and bInput == 1:  
    ORGate = "True"  
    ORGateNum = 1  
else:  
    ORGate = "False"  
    ORGateNum = 0  

print('OR Gate output is', ORGate, 'or', ORGateNum) 
...