简体   繁体   English

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

[英]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, and give any value for the other constants that you want.但是,我想要它,以便您可以求解您想要的任何变量,并为您想要的其他常量提供任何值。

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)

This is of course incomplete, because the values for a,b,c,and d aren't assigned.这当然是不完整的,因为没有分配 a、b、c 和 d 的值。 The way I have it setup so far, for input 1, if the user wants to solve for a variable, they simply type that variable name in, if they want to assign a value to the variable, they input the value of the variable.到目前为止,我的设置方式是,对于输入 1,如果用户想求解一个变量,他们只需输入变量名,如果他们想为变量赋值,则输入变量的值。 The issue comes down to, how can I set it up so the user can assign values to the constants table?问题归结为,我如何设置它以便用户可以为常量表分配值? IE IE

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]

The above doesn't work, but the logic is:以上不起作用,但逻辑​​是:

#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 

or another method, perhaps simpler/cleaner:或另一种方法,也许更简单/更清洁:

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

or something of that similar nature.或类似性质的东西。 But of course, this doesn't define them either.但当然,这也没有定义它们。 I was thinking maybe you could create a dictionary, where a:1, b:2, and c:3, but at this point I'm just throwing ideas around and shooting in the dark.我在想也许你可以创建一个字典,其中 a:1、b:2 和 c:3,但在这一点上,我只是四处乱扔想法并在黑暗中拍摄。 So any feedback would be greatly appreciated!所以任何反馈将不胜感激!

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 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)

Basically we build up a dict and pass that to subs:基本上我们建立一个 dict 并将其传递给 subs:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM