我試圖寫一個程序作爲一個練習從計算積分值與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
編輯:解決 我通過一次我沒有在所有調用它調用一個函數犯了一個錯誤。
例如'˚F==「在這一點上g'' - 這只是一個單一的字符串,是什麼你期望叫它做什麼? – jonrsharpe
只是要清楚,錯誤是在'打印'定積分是',整合(funcIn,aIn,bIn,nIn)',而不是原來的整合。 'funcIn'是字符串,而不是'g'或'h'本身。 –