编程语言
首页 > 编程语言> > 使用Python中的用户输入来调用/选择变量(浮点值)

使用Python中的用户输入来调用/选择变量(浮点值)

作者:互联网

我一直在从事计算物理项目(将化学反应物相对于彼此的比率作图以显示振荡行为),并取得了相当大的成功.但是,我的模拟之一涉及两个以上的有源振动剂(实际上是五个),显然不适合任何单个视觉图…

因此,我的方案是让用户选择他们想要在x轴和y轴上绘制的两种反应物.我试图(愚蠢地)将字符串输入值转换为相应的变量名,但是我想我需要一种根本不同的方法(如果存在)?

如果它有助于弄清任何内容,这是我的代码的一部分:

def coupledBrusselator(A, B, t_trial,display_x,display_y):
    t = 0
    t_step = .01
    X = 0       
    Y = 0
    E = 0
    U = 0
    V = 0
    dX = (A) - (B+1)*(X) + (X**2)*(Y)  
    dY = (B)*(X) - (X**2)*(Y)
    dE = -(E)*(U) - (X)  
    dU = (U**2)*(V) -(E+1)*(U) - (B)*(X)
    dV = (E)*(U) - (U**2)*(V)
    array_t = [0]
    array_X = [0]
    array_Y = [0]
    array_U = [0]
    array_V = [0]       

    while t <= t_trial:             
        X_1 = X + (dX)*(t_step/2)   
        Y_1 = Y + (dY)*(t_step/2)
        E_1 = E + (dE)*(t_step/2)
        U_1 = U + (dU)*(t_step/2)
        V_1 = V + (dV)*(t_step/2) 
        dX_1 = (A) - (B+1)*(X_1) + (X_1**2)*(Y_1)  
        dY_1 = (B)*(X_1) - (X_1**2)*(Y_1)
        dE_1 = -(E_1)*(U_1) - (X_1)  
        dU_1 = (U_1**2)*(V_1) -(E_1+1)*(U_1) - (B)*(X_1)
        dV_1 = (E_1)*(U_1) - (U_1**2)*(V_1)
        X_2 = X + (dX_1)*(t_step/2)
        Y_2 = Y + (dY_1)*(t_step/2)
        E_2 = E + (dE_1)*(t_step/2)
        U_2 = U + (dU_1)*(t_step/2)
        V_2 = V + (dV_1)*(t_step/2) 
        dX_2 = (A) - (B+1)*(X_2) + (X_2**2)*(Y_2)
        dY_2 = (B)*(X_2) - (X_2**2)*(Y_2)
        dE_2 = -(E_2)*(U_2) - (X_2)  
        dU_2 = (U_2**2)*(V_2) -(E_2+1)*(U_2) - (B)*(X_2)
        dV_2 = (E_2)*(U_2) - (U_2**2)*(V_2)   
        X_3 = X + (dX_2)*(t_step)
        Y_3 = Y + (dY_2)*(t_step)
        E_3 = E + (dE_2)*(t_step)
        U_3 = U + (dU_2)*(t_step)
        V_3 = V + (dV_2)*(t_step) 
        dX_3 = (A) - (B+1)*(X_3) + (X_3**2)*(Y_3)
        dY_3 = (B)*(X_3) - (X_3**2)*(Y_3)
        dE_3 = -(E_3)*(U_3) - (X_3)  
        dU_3 = (U_3**2)*(V_3) -(E_3+1)*(U_3) - (B)*(X_3)
        dV_3 = (E_3)*(U_3) - (U_3**2)*(V_3) 
        X = X + ((dX + 2*dX_1 + 2*dX_2 + dX_3)/6) * t_step 
        Y = Y + ((dX + 2*dY_1 + 2*dY_2 + dY_3)/6) * t_step
        E = E + ((dE + 2*dE_1 + 2*dE_2 + dE_3)/6) * t_step          
        U = U + ((dU + 2*dU_1 + 2*dY_2 + dE_3)/6) * t_step
        V = V + ((dV + 2*dV_1 + 2*dV_2 + dE_3)/6) * t_step
        dX = (A) - (B+1)*(X) + (X**2)*(Y)  
        dY = (B)*(X) - (X**2)*(Y)
        t_step = .01 / (1 + dX**2 + dY**2) ** .5
        t = t + t_step
        array_X.append(X) 
        array_Y.append(Y)
        array_E.append(E)
        array_U.append(U)
        array_V.append(V)
        array_t.append(t)   

以前在哪里

display_x = raw_input("Choose catalyst you wish to analyze in the phase/field diagrams (X, Y, E, U, or V) ") 
display_y = raw_input("Choose one other catalyst from list you wish to include in phase/field diagrams ")

coupledBrusselator(A, B, t_trial, display_x, display_y) 

谢谢!

解决方法:

计算完不同的数组后,可以将它们添加到将名称映射到数组的字典中.然后可以使用它为display_x和display_y查找正确的数组:

named_arrays = {
  "X": array_X,
  "Y": array_Y,
  "E": array_E,
  ...
}

return (named_arrays[display_x], named_arrays[display_y])

标签:raw-input,python,variables,floating-point,input
来源: https://codeday.me/bug/20191012/1904103.html