2016-11-23 51 views
0

我試圖寫一個程序作爲一個練習從計算積分值與2個數學函數一個爲b。我的函數集成應該有作爲數學函數的集成。「海峽」對象不是可調用,功能ARGS

from math import * 

def g(x): 
     return float(x) * float(x) + 3 

def h(x): 
    return math.cos(float(x) * float(x)) 

def integrate(f, a, b, n): 
    H = (abs(float(a) - float(b)))/float(n) 
    ans = 0 
    xWaarde = a - H/2 
    print xWaarde 
    for k in range(1, n+1): 
     xWaarde = xWaarde + H 
     ans = ans + f(xWaarde) * H 
    return ans 

print 'available functions:' 
print 'g(x) = x^2+3' 
while True: 
    print 'h(x) = cos(x^2)' 
    aIn = float(raw_input('integral from a = ')) 
    bIn = float(raw_input('to b = ')) 
    nIn = int(raw_input('Number of subintervals: ')) 
    while True: 
     funcIn = raw_input('Which function do you want to use? (g or h): ') 
     if funcIn == 'g': 
      integrate(g,aIn,bIn,nIn) 
      break 
     elif funcIn == 'h': 
      integrate(h,aIn,bIn,nIn) 
      break 
     else: 
      print 'This function is not available' 


    print 'The definite integral is', integrate(funcIn, aIn, bIn, nIn) 
    doorg = raw_input('Do you want to continue? (y or n): ') 
    if doorg == 'n': 
     break 
    else: 
     print 

完整回溯如下:

Traceback (most recent call last): 
    File "C:/Users/Nick van Stijn/Desktop/Python/Assignment 3.1.py", line 38, in <module> 
    print 'The definite integral is', integrate(funcIn, aIn, bIn, nIn) 
    File "C:/Users/Nick van Stijn/Desktop/Python/Assignment 3.1.py", line 16, in integrate 
    ans = ans + f(xWaarde) * H 
TypeError: 'str' object is not callable 

編輯:解決 我通過一次我沒有在所有調用它調用一個函數犯了一個錯誤。

+0

例如'˚F==「在這一點上g'' - 這只是一個單一的字符串,是什麼你期望叫它做什麼? – jonrsharpe

+0

只是要清楚,錯誤是在'打印'定積分是',整合(funcIn,aIn,bIn,nIn)',而不是原來的整合。 'funcIn'是字符串,而不是'g'或'h'本身。 –

回答

1

的問題是,你使用正確的功能,fg致電integrate,但隨後丟棄的結果,而不是調用integrate再次的打印,此時路過只是名稱的功能的funcIn

相反,你應該只把結果保存在一個變量,例如,像這樣:

result = None 
while result is None: 
    funcIn = raw_input('Which function do you want to use? (g or h): ') 
    if funcIn == 'g': 
     result = integrate(g,aIn,bIn,nIn) 
    elif funcIn == 'h': 
     result = integrate(h,aIn,bIn,nIn) 
    else: 
     print 'This function is not available' 

print 'The definite integral is', result 

此外,您還可以使用dict使用可能大量的函數名,以實際的功能映射,而不是的if/elif/else

functions = {'h': h, 'g': g} 
while result is None: 
    funcIn = raw_input('Which function do you want to use? (g or h): ') 
    if funcIn in functions: 
     result = integrate(functions[funcIn],aIn,bIn,nIn) 
    else: 
     print 'This function is not available' 
+0

這是實際解釋錯誤發生原因的唯一答案。 –

+0

是的,謝謝你,這是一個愚蠢的錯誤,我使用的代碼是從我以前的練習中複製而來的,「print」這一行應該被刪除。 – TheNickqq

1

你正在一個字符串的形式使用功能的文本名稱,而不是函數對象本身的引用。儘管從字符串名稱派生函數對象的方法很棘手,但它們很難維護並且容易出錯。由於python函數中的對象與其他任何對象(所謂的「第一類」對象)不同,因此它們並沒有真正的名稱,只有引用才能使函數具有名稱。

這是一本字典就派上用場了,特別是如果你想在以後添加更多功能的一個很好的例子。我們可以映射文本鍵(用戶輸入的)任何Python對象,其中一個功能:

from math import * 

def g(x): 
     return float(x) * float(x) + 3 

def h(x): 
    return math.cos(float(x) * float(x)) 

# Store references to the functions in a dictionary 
# with the keys as the text name (the names need not match) 
funcs = {'g': g, 'h': h}  # <<<< ADDED 

def integrate(f, a, b, n): 
    H = (abs(float(a) - float(b)))/float(n) 
    ans = 0 
    xWaarde = a - H/2 
    print xWaarde 
    for k in range(1, n+1): 
     xWaarde = xWaarde + H 
     ans = ans + f(xWaarde) * H 
    return ans 

print 'available functions:' 
print 'g(x) = x^2+3' 
while True: 
    print 'h(x) = cos(x^2)' 
    aIn = float(raw_input('integral from a = ')) 
    bIn = float(raw_input('to b = ')) 
    nIn = int(raw_input('Number of subintervals: ')) 
    while True: 
     funcIn = raw_input('Which function do you want to use? (g or h): ') 

     # THIS CODE CHANGED - note the simplification 
     # we just test for membership of the dictionary 
     if funcIn in funcs: 
      integrate(funcs[funcIn],aIn,bIn,nIn) 
      break 
     else: 
      print 'This function is not available' 

    # THIS CODE CHANGED (note first argument to integrate) 
    print 'The definite integral is', integrate(funcs[funcIn], aIn, bIn, nIn) 
    doorg = raw_input('Do you want to continue? (y or n): ') 
    if doorg == 'n': 
     break 
    else: 
     print 
+1

雖然這解決了OP的問題,並引入了一種更好的技術,但注意行'print'的定積分是',整合(funcs [funcIn],aIn,bIn,nIn)'是原始代碼中打破的東西會好的。 –

+0

@MadPhysicist:公平的評論。我沒有包括它,因爲錯誤的原因已經很好解釋了。我的目的只是爲了展示更好的技術。 – cdarke

相關問題