【发布时间】:2012-08-04 13:12:19
【问题描述】:
我正在尝试将变量数学运算符插入到 if 语句中,这是我在解析用户提供的数学表达式时尝试实现的示例:
maths_operator = "=="
if "test" maths_operator "test":
print "match found"
maths_operator = "!="
if "test" maths_operator "test":
print "match found"
else:
print "match not found"
显然以上失败了SyntaxError: invalid syntax。我尝试过使用 exec 和 eval,但在 if 语句中都不起作用,我有什么选项可以解决这个问题?
【问题讨论】:
标签: python parsing if-statement operators mathematical-expressions