I need to know how transfer string input into executable function. For example - user write string 'x*Sin(x**2)' and then programm takes it as function, can calculate a value for given x, can plot derivation of this function etc. I've read that there is module called scitools.stringfunction, but as far as I know this module is not callable in python-3.
Any ideas how to make it?
For Python 2.X
For Python 3.X
Just make sure to
import
the required mathematical functions. And make sure the user inputs a valid Python arithmetic expression.using sympy you could do something like this:
if you want to turn the user input into a function you can call you could to this:
sympy can do sybolic calculations (including derivatives); if you want to make plots i strongly suggest
matplotlib
.the advantage of using a math library opposed to
eval
is that you do not need to sanitize the user input (against malicious code).