【发布时间】:2017-11-11 11:31:40
【问题描述】:
我已经制作了一个逻辑门,它接收两个输入,然后在这种情况下将其馈入 AND 门。例如,用户将输入 0 和 0,然后与门将处理为 0。
这里的问题是,当我们使用 IF 语句来确定我们的两个输入时,它们不会被识别,否则程序中的所有其他东西都会处理这两个输入以及输出的临时存储。
A = input("Enter a value of 1 or 0: ")
B = input("Enter a value of 1 or 0: ")
print(A)
print(B)
所以上面的部分我可以输入输入并为它创建一个存储。 程序告诉我 A 和 B 无法识别,所以有人知道我在这里做错了什么吗?
这就是问题所在:从这里到 else 语句的所有内容都被忽略了。
def first_AND ():
if A == 0 and B == 0:
AND_1()
print(AND)
print("Now solve the XOR gate")
gate_path_A()
elif A == 1 and B == 0:
AND_2()
print(AND)
print("Now solve the XOR gate")
gate_path_A()
elif A == 0 and B == 1:
AND_3()
print(AND)
print("Now solve the XOR gate")
gate_path_A()
elif A == 1 and B == 1:
AND_4()
print(AND)
print("Now solve the XOR gate")
gate_path_A()
else:
print("Error")
它会跳过我所有的 elif 语句,只打印一个错误。
def AND_1():
print(A & " AND " & B & " = 0")
AND = 0
def AND_2():
print(A & " AND " & B & " = 0")
AND = 0
def AND_3():
print(A & " AND " & B & " = 0")
AND = 0
def AND_4():
print(A & " AND " & B & " = 1")
AND = 1
【问题讨论】:
-
prints an error- 你介意给出错误吗? -
缩进在 python 中很重要 - 也许你的 def 代码必须移动一点?
-
如果您有任何不清楚的地方,请务必留言,我会尽快回复您。
-
是的,没问题,帕特里克
-
文件“C:/Users/waliu/Desktop/test.py”,第 33 行,在
first_AND() 文件“C:/Users/waliu/Desktop/test.py”中,第 21 行,在 first_AND AND_1() 文件 "C:/Users/waliu/Desktop/test.py",第 27 行,在 AND_1 print(A & " AND " & B & " = 0") 类型错误:不支持的操作数类型( s) for &: 'str' 和 'str'