Search code examples
pythontext-recognition

Text to Math equation in Python


I'm writing now a pet project in which I want to draw a plot in the terminal window. The idea is that user print an equation, like x = y + 1, or 3 * x = y. The question is how can I transfer this string equation to an equation in Python.

For instance: I have an array, for x values like:

x_arr = [1, 2, 3, 4, 5]

And then I want to put it into the user's equation to get y values. or vise versa (vise versa, put y values to get x values)

Thought to create a lot of if conditions and use for loop to detect every character but it seems something like a sandbox. Is there a more elegant way?


Solution

  • Use eval for evaluating the textual commands.

    https://www.programiz.com/python-programming/methods/built-in/eval