我有代码:
import re
import math
#from sympy import * # breaks?
#x, y, z = symbols('x y z')
#init_printing(use_unicode=True)
def evaluate_expression(expr, variables):
# Evaluate the expression using eval(), and provide the variables dictionary
return eval(expr, variables)
def main():
tools = """
<<<[eval("5"),a_x]>>>
<<<[eval("5+a_x"),ans]>>>
"""
variables = {}
lines = tools.strip().split("\n")
for line in lines:
# Extract the inner array using regular expression
match = re.search(r'<<<\[(.*?),(\w+)\]>>>', line)
if match:
# Extract expression and variable name
expr = match.group(1)
var_name = match.group(2)
# Evaluate expression
value = evaluate_expression(expr, variables)
# Update variables dictionary
variables[var_name] = value
# Check if 'ans' is defined
if 'ans' in variables:
print("ans =", variables['ans'])
break
if __name__ == "__main__":
main()
如果我不导入symy,它运行得很好,但如果我确实导入symy,它就会崩溃,重新搜索不再存在,无论我给变量命名什么.我不明白吗?