I’m writing a program that involves calculating the maximum of a user defined function, however if the user enters a function in terms of a variable such as x, i.e.
input = x**2 + 2*x + 1
I (fairly expectedly) get an error as x is not defined.
Does anyone know of an effective way to allow a user to input a function in terms of a single variable such as x?
If you are not worried about security much, simplest solution is to
evalthe expression e.g.output:
You can make it more secure by passing an empty
builtindict but still I think some clever code will be able to access internals of your code.