【问题标题】:Logic Gate implementation fails逻辑门实施失败
【发布时间】: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'

标签: python logic


【解决方案1】:

我为你清理了你的代码:你应该阅读 python 语法,f.e. https://docs.python.org/2/reference/index.html(适用于 2.7.x)并做一些教程

# define global 
AND = None

#define the used functions
def gate_path_A():
    print("Reached gate_path_A()")
    return


def first_AND (A,B):
    if A == 0 and B == 0: 
        AND_1(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 1 and B == 0:
        AND_2(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 0 and B == 1:
        AND_3(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    elif A == 1 and B == 1:
        AND_4(A,B)
        print(AND)
        print("Now solve the XOR gate")
        gate_path_A()
    else: 
        print("Error")
    return  


def AND_1(A,B):
    print(A , " AND " , B , " = 0")
    AND = 0
    return

def AND_2(A,B):
    print(A , " AND " , B ," = 0")
    AND = 0
    return

def AND_3(A,B):
    print(A , " AND " , B , " = 0")
    AND = 0
    return

def AND_4(A,B):
    print(A , " AND " , B , " = 1")
    AND = 1
    return

主程序

# get one integer from user
a = None
while a is None:
    try:
        a = int(input("Enter a number: "))
    except ValueError:
        print("Not an integer value...")
print(str(a));

# get second integer from user 
# you should really put this in a def and return the integer: DRY principle
b = None
while b is None:
    try:
        b = int(input("Enter a number: "))
    except ValueError:
        print("Not an integer value...")
print(str(b));

# call to the first and thing and supply the params your got from user
first_AND(a,b);

【讨论】:

  • 老实说,我想将 A 和 B 的输入作为函数传递。所以基本上一旦我写了我的函数,我就必须在它上面写一个 return 语句?
  • 这就是我一直在寻找的问题:NameError: name 'A' is not defined - 这就是问题所在,同样适用于 B。这就是我写的我的功能你建议帕特。
  • def inputs(A, B): A = input("请输入 1 或 0 的值:") B = input("请输入 1 或 0 的值:") return A return B
  • 查找如何创建函数,例如here - 你可以返回 smthif 你喜欢 - 或返回 None。
  • 哇,谢谢 patrick 我会在这里测试你的代码,看看它是否能完美地完成我打算做的事情以前的门到我们的下一个门并存储其他门的结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2021-09-12
  • 1970-01-01
相关资源
最近更新 更多