2014-09-28 56 views
-3

現在我一直在研究這個程序來模擬python中滑塊曲柄機構的運動。 我遇到這個錯誤時遇到了死衚衕。這是無緣無故拋出錯誤的方法。TypeError:'int'對象不可調用,滑塊曲柄機制

def returnSliderAcceleration(a,b,theta_1,omega_1,alpha_1): 
    rad_temporary = theta_1 * np.pi/180 
    rad_temporary_2 = returnConnectorAngle(a,b,theta_1) 
    omega_2 = returnConnectorVelocity(a,b,theta_1,omega_1) 
    alpha_2 = returnConnectorAcceleration(a,b,theta_1,omega_1,alpha_1) 
    part_1 = a((alpha_1 * np.sin(rad_temporary)) + ((omega_1**2)*np.cos(rad_temporary))) 
    part_2 = b((alpha_2 * np.sin(rad_temporary_2)) + ((omega_2**2)*np.cos(rad_temporary_2))) 
    slider_acceleration = -1*(part_1 + part_2) 
    return slider_acceleration 

它利用的另一方法是:

def returnConnectorAngle(a,b,theta_1): #Returns the angle(in degrees) made by the connector corresponding to the crank angle, CCW taken as positive 
    rad_temporary = theta_1 * np.pi/180 
    x = (a/b) * np.sin(rad_temporary) 
    connector_angle = np.arcsin(x) 
    return (connector_angle* np.pi/180) 

def returnConnectorVelocity(a,b,theta_1,omega_1): #Returns the angular velocity of the connector, CCW taken as positive 
    rad_temporary = theta_1 * np.pi/180 
    rad_temporary_2 = returnConnectorAngle(a,b,theta_1) 
    Nr = a * omega_1 * np.cos(rad_temporary) 
    Dr = b * np.cos(rad_temporary_2) 
    connector_velocity = Nr/Dr 
    return connector_velocity 

def returnConnectorAcceleration(a,b,theta_1,omega_1,alpha_1): #Returns the angular acceleration of the connector arm, CCW taken as positive 
    rad_temporary = theta_1 * np.pi/180 
    rad_temporary_2 = returnConnectorAngle(a,b,theta_1) 
    omega_2 = returnConnectorVelocity(a,b,theta_1,omega_1) 
    Nr_1 = a * ((alpha_1 * np.cos(rad_temporary)) - ((omega_1**2)*np.sin(rad_temporary))) 
    Dr_1 = b * np.cos(rad_temporary_2) 
    part_2 = (omega_2**2) * np.tan(rad_temporary_2) 
    return (Nr_1/Dr_1) + part_2 

的錯誤:

slider_acceleration = -1*(a((alpha_1 * np.sin(rad_temporary)) + ((omega_1**2)*np.cos(rad_temporary)))+ b((alpha_2 * np.sin(rad_temporary_2)) + ((omega_2**2)*np.cos(rad_temporary_2)))) 
TypeError: 'int' object is not callable 
+0

請格式化你的代碼,這是一個真正的痛苦,這樣做... – 2014-09-28 05:37:29

+0

對不起。我對這個地方很陌生,當你爲我做的時候我只是在編輯。謝謝! – 2014-09-28 05:49:42

回答

3

之前,我們甚至看你的代碼,讓我們在錯誤仔細看看Python是給你:

TypeError: 'int' object is not callable 

In thi s case,你的錯誤是TypeError。這意味着某些Python類型使用不正確。更詳細的描述告訴我們另外兩個事實:

  1. 使用不正確的類型是一個int
  2. 的問題是,我們試圖呼叫int,但我們不能做但─我們可以調用functionsmethods,但不ints

下面就來產生相同的問題,一個簡單的辦法:

$ python 
>>> x = 3 
>>> y = x(10) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'int' object is not callable 

我們得到了這個錯誤,因爲我們試圖使用x作爲參數10的函數,但x只是一個int。現在我們明白了這個錯誤的含義,讓我們來看看您的代碼。這裏的問題的行,分裂了幾行字:

slider_acceleration = -1 * (
    a(
     (alpha_1 * np.sin(rad_temporary)) + \ 
      ((omega_1**2)*np.cos(rad_temporary)) 
    ) + b(
     (alpha_2 * np.sin(rad_temporary_2)) + \ 
      ((omega_2**2)*np.cos(rad_temporary_2)) 
    ) 
) 

它看起來像你使用ab的功能,但他們?快速看看它們是如何用於其他功能的,我不會懷疑。也許你的意思是乘他們是這樣的:

slider_acceleration = -1 * (
    a * (
     (alpha_1 * np.sin(rad_temporary)) + \ 
      ((omega_1**2)*np.cos(rad_temporary)) 
    ) + b * (
     (alpha_2 * np.sin(rad_temporary_2)) + \ 
      ((omega_2**2)*np.cos(rad_temporary_2)) 
    ) 
) 
+1

啊,解釋所有這一切。我只是搞砸了基本的語法。我在紙上解決了問題,然後在轉換爲代碼時,我只是在紙上鍵入了那些內容。萬分感謝! – 2014-09-28 07:01:37