1

I have a .txt file that has the following text:

"np.sqrt(2)**2"

I can't get the answer to this mathematical equation because is a string, does anyone know how to convert that text to code (In python)? So when executing the script I will have the following output:

[In] np.sqrt(2)**2
[Out] 2

1 Answers1

0

You can use the eval() method. The return value is the result of the evaluated expression.

>>> x = 1
>>> eval('x + 1')
2
>>> eval('x')
1
dot.Py
  • 5,007
  • 5
  • 31
  • 52