Reputation: 11
What I want is a program that can determine the value of x
from an equation when x
is not yet defined i.e. not a python variable.
Just an example below, not the real thing.
sol = eval("input please type the equation: ")
#i.e sol = 32x - 40
print(sol)
Upvotes: 1
Views: 10922
Reputation: 1
You can just use sympy. Then you can do it in the print command. It looks like this.
import sympy
from sympy.abc import x
print sympy.solve(nub1*x+nub2-nub3*x,"Whatever you want here.")
Upvotes: 0
Reputation: 88278
An explicit example using sympy
import sympy
from sympy.abc import x
print sympy.solve(32*x-40,"x")
print sympy.solve(2*x+23-7*x,"x")
Gives as output:
[5/4]
[23/5]
Note that there is the separate question of parsing user input. That is, how do we take the string "32x-40" and turn it into the expression 32*x-40
. This can be a non-trivial task depending on the complexity of the equations you are looking to model. If you are insterested in that, I would look into pyparsing
.
Upvotes: 1
Reputation: 11470
I am not aware of any built in way to do that but Sympy
library is built exactly for this stuff. Solvers module in Sympy can be used to solve linear equations. (Here) is a link to its docs.
Upvotes: 2