Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用用户输入定义变量值_Python_Sympy - Fatal编程技术网

Python 如何使用用户输入定义变量值

Python 如何使用用户输入定义变量值,python,sympy,Python,Sympy,我遇到了一个我不太知道如何解决的问题。我试着用解算器来解一个简单的方程。然而,我想要它,这样你就可以为你想要的任何变量求解,并为你想要的其他常量提供任何值 from sympy import * a, b, c, d=symbols('a b c d') constants=[] input1=list(input('input variables for a,b,c,and d')) for value in input1: try: int_values=i

我遇到了一个我不太知道如何解决的问题。我试着用解算器来解一个简单的方程。然而,我想要它,这样你就可以为你想要的任何变量求解,并为你想要的其他常量提供任何值

from sympy import *
a, b, c, d=symbols('a b c d')

constants=[]
input1=list(input('input variables for a,b,c,and d'))
for value in input1:
    try:    
        int_values=int(value)
        constants.append(int_values)
    except: 
        solve_for=value

equation=solveset(a + (b-1) * c-d, solve_for)
print (equation)
这当然是不完整的,因为没有分配a、b、c和d的值。到目前为止,我的设置方式是,对于输入1,如果用户想要求解变量,他们只需输入变量名,如果他们想给变量赋值,他们就输入变量的值。问题归根结底是,我如何设置它,以便用户可以为常量表分配值?即

def_var_vale=list(input('define what variables are in constants'))
def_var_value[0],def_var_value[1],def_var_value[2]=constants[0],constants[1],constants[1]
上述方法不起作用,但逻辑是:

#input
def_var_value=[b, c, d]
constants=[1,2,3]
#desired output
b=1
c=2
d=3
# defined variables and their values to be used for the equation 
或者另一种方法,可能更简单/更干净:

for letter,number in zip(def_var_value,constants):
      letter=number

或者类似的东西。当然,这也不能定义它们。我在想也许你可以编一本字典,里面有a:1,b:2和c:3,但在这一点上,我只是在胡思乱想,在黑暗中拍摄。因此,任何反馈都将不胜感激

我不确定,但也许这就是你想要的:

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 in zip(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)
基本上,我们建立一个dict并将其传递给SUB:

In [1]: (x + 2*y)                                                                                                                 
Out[1]: x + 2⋅y

In [2]: (x + 2*y).subs({x: 5})                                                                                                    
Out[2]: 2⋅y + 5

我不确定,但也许这就是你想要的:

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 in zip(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)
基本上,我们建立一个dict并将其传递给SUB:

In [1]: (x + 2*y)                                                                                                                 
Out[1]: x + 2⋅y

In [2]: (x + 2*y).subs({x: 5})                                                                                                    
Out[2]: 2⋅y + 5

正是我想要的。非常感谢。正是我想要的。非常感谢。