python and or xor_Python逻辑代码:AND、NAND、OR、NOR、XOR、XNOR和NOT模拟

我想弄清楚我的代码为什么不能工作。为什么我的一些逻辑门,比如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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值