我试图编写一个程序作为练习,用2个数学函数之一计算从a到b的积分值。我的函数集成应该有f作为集成的数学函数。
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 :(得分:1)
问题是您使用正确的函数integrate
或f
来呼叫g
,然后丢弃结果,而是拨打{{1} 再次用于打印,这次只传递函数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
答案 1 :(得分:1)
您正在以字符串的形式使用函数的文本名称,而不是对函数对象本身的引用。虽然有一些hacky技术从字符串名称派生函数对象,但它们很难维护并且容易出错。由于在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