Python 3.x 如果参数不是';我还不知道呢?
此代码不运行。它在运行(闸门)功能期间失败。我得到的第一个错误来自Python 3.x 如果参数不是';我还不知道呢?,python-3.x,Python 3.x,此代码不运行。它在运行(闸门)功能期间失败。我得到的第一个错误来自。\uuuu name\uuuu调用。这不是我的主要问题,所以我并不特别担心这个错误 我得到的第二个错误是在运行的最后一行(gate)。错误消息说gate不可调用 我相信我的实际问题在于我的课程结构。我是否误解(或误用)了一些面向对象的原则?如果有人能为我的代码提供一个示例或更正,以便更好地构建它,我将不胜感激。谢谢大家! main.py import builtins import logic print("Select a
。\uuuu name\uuuu
调用。这不是我的主要问题,所以我并不特别担心这个错误
我得到的第二个错误是在运行的最后一行(gate)。错误消息说gate不可调用
我相信我的实际问题在于我的课程结构。我是否误解(或误用)了一些面向对象的原则?如果有人能为我的代码提供一个示例或更正,以便更好地构建它,我将不胜感激。谢谢大家!
main.py
import builtins
import logic
print("Select a logic gate:")
print("1) AND")
print("2) NAND")
print("3) OR")
print("4) NOR")
print("Q) Quit Program")
logicGate = input()
if(int(logicGate) == 1):
run(logic.andGate)
elif(int(logicGate) == 2):
run(logic.nandGate)
elif(int(logicGate) == 3):
run(logic.orGate)
elif(int(logicGate) == 4):
run(logic.norGate)
elif(logicGate.lower() == 'q'):
prog = 'n'
else:
print("Invalid input. Please try again")
def toBool(s):
if s == 'True':
return True
elif s == 'False':
return False
else:
raise ValueError
def run(gate):
print(gate.__name__ + " Gate function")
print("Enter value for 'a'")
valA = input()
print("Enter value for 'b'")
valB = input()
print("Result: " + str(gate(toBool(valA), toBool(valB))))
和logic.py
def andGate(a, b):
if(a and b):
return True
else:
return False
def nandGate(a,b):
if(a and b):
return False
else:
return True
def orGate(a, b):
if(a or b):
return True
else:
return False
def norGate(a, b):
if(a or b):
return False
else:
return True
将您的DEF移动到导入下,只需删除else:raise。。。。等等等等,等等。。。它没有正确使用
import builtins
import logic
def toBool(s):
if s == 'True':
return True
elif s == 'False':
return False
def run(gate):
print(gate.__name__ + " Gate function")
print("Enter value for 'a'")
valA = input()
print("Enter value for 'b'")
valB = input()
print("Result: " + str(gate(toBool(valA), toBool(valB))))
print("Select a logic gate:")
print("1) AND")
print("2) NAND")
print("3) OR")
print("4) NOR")
print("Q) Quit Program")
logicGate = input()
if(int(logicGate) == 1):
run(logic.andGate)
elif(int(logicGate) == 2):
run(logic.nandGate)
elif(int(logicGate) == 3):
run(logic.orGate)
elif(int(logicGate) == 4):
run(logic.norGate)
elif(logicGate.lower() == 'q'):
prog = 'n'
else:
print("Invalid input. Please try again")
如果在导入之后将
run
和toBool
移动到脚本的顶部,则工作正常。@cᴏʟᴅsᴘᴇᴇᴅ 是的,即使如此,为什么?打印结果时??如果它的布尔对象不应该是整数。。。0或1。。。或真或假。。。重点是。。。。这将提高这一点ValueError@scriptsostr(真);'True'righ t但字符串始终为True lol。。。即使x=“假”。。。是true@cᴏʟᴅsᴘᴇᴇᴅ 哦,老兄,达普。。。。注意到异常是如何产生的吗?对不起,没有pep!希望它不是乱七八糟的。