While using sympy.solve() to try and solve simple linear equations, the function just returns an empty list.
Here is my code:
from sympy import Symbol
from sympy.solvers import solve
from sympy.parsing.sympy_parser import parse_expr as parse
x = Symbol('x')
equation = input('Enter an equation: ')
equation = message.content.split()[1].replace('=',',')
solution = solve(parse(equation))
For equation = 'x 3=8'
, print(solution)
just prints []
Can anyone figure out why this is happening?
Thanks!
CodePudding user response:
I'm not sure what the best practice here is, but using Eq
here will work.
The code after a few changes-
from sympy import Symbol, Eq
from sympy.solvers import solve
from sympy.parsing.sympy_parser import parse_expr as parse
x = Symbol('x')
equation_1 = input('Enter an equation: ')
equation_2 = equation_1.split('=')
solution = solve(Eq(*[parse(i) for i in equation_2]), x)
print(solution)
example output-
Enter an equation: x 1=x**2
[1/2 - sqrt(5)/2, 1/2 sqrt(5)/2]