How To Define Variable Values Using User Input
I have come across an issue I don't quite know how to solve. I'm trying to use solver to solve a simple equation. However, I want it so that you can solve for any variable you want
Solution 1:
I'm not sure but maybe this is what you want:
from sympy import *
a, b, c, d = syms = symbols('a b c d')
eq = a + (b-1) * c-d
constants=[]
input_string = '1 2 3 d'
input_words = input_string.split()
rep = {}
for word, sym inzip(input_words, [a,b,c,d]):
if word.isalpha():
solve_for = sym
else:
rep[sym] = int(word)
soln = solveset(eq.subs(rep), solve_for)
print(soln)
Basically we build up a dict and pass that to subs:
In [1]: (x + 2*y)
Out[1]: x + 2⋅y
In [2]: (x + 2*y).subs({x: 5})
Out[2]: 2⋅y + 5
Post a Comment for "How To Define Variable Values Using User Input"