I have the code:
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()
If I dont import sympy, it runs fine, but if I do import sympy, it breaks, the re search no longer exists, no matter what I name variables. I dont get it?
sympy
has re
class
, when you import all *
you hide the re
import. Import sympy
first (or import only what you actually need from it)
from sympy import *
import re
# Output: ans = 10