【发布时间】:2018-11-18 01:12:30
【问题描述】:
我正在尝试找出我的代码无法正常工作的原因。为什么我的一些逻辑门,比如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)
我还尝试将aInput 和bInput 之间的and 替换为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)
【问题讨论】:
-
非常漂亮的图片。但请发布文字,而不是图片。
-
我看到你正在使用 if if if else。尝试使用 if elif elif else 代替。如果不清楚原因,请尝试此链接以获取更多信息:stackoverflow.com/questions/9271712/…
-
@alfonso 还没学会。我试试看,谢谢!
-
我回答了你的问题,因为我很同情迟到并且不知道如何完成你的作业,但请稍后再回来编辑你的问题。在 SO,我们努力使网站达到一定的标准,以造福于使用它的每个人。
-
@alfonso 谢谢!我想出了如果我想展示一小段代码我必须做什么。
标签: python function boolean logic boolean-logic