我想用eval做操作来查看是否a和b等于c某个运算符。
我的代码:
def Expression(a, b, c):
operators = ["+", "-", "*", "/"]
return len([i for i in operators if eval("a () b".replace("()", i)) == c]) >= 1
Expression(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这会导致 NameError。错误日志:
return len([i for i in operators if eval("a () b".replace("()", i)) == c]) >= 1
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
由于该函数a作为参数,我认为a不应未定义。这里有什么问题?