Python Logic Code: AND, NAND, OR, NOR, XOR, XNOR, & NOT模拟
Python Logic Code: AND, NAND, OR, NOR, XOR, XNOR, & NOT simulation
我想弄清楚为什么我的代码不起作用。为什么我的一些逻辑门,例如 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)
我还尝试用 or
替换 aInput
和 bInput
之间的 and
,这似乎有效,但有点难以重复使用 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)
如评论中所述,请尝试使用 elif
。欢迎来到 Stack Overflow!
#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)
您可能还对 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
我想弄清楚为什么我的代码不起作用。为什么我的一些逻辑门,例如 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)
我还尝试用 or
替换 aInput
和 bInput
之间的 and
,这似乎有效,但有点难以重复使用 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)
如评论中所述,请尝试使用 elif
。欢迎来到 Stack Overflow!
#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)
您可能还对 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