我知道这个错误通常是因为你混淆了一个函数和一个名字,一类问题。我似乎无法弄明白。
#P1
def fracSteps(N):
""" fracSteps returns a list of N evenly spaced floating-point values starting at 0.0 and going up to, but not including 1.0
Input: N will be a positive integer
"""
return [ x/N for x in range(N) ]
#P2
def steps(low,hi,N):
""" steps returns a regularly-spaced list of N floats, starting at low and going up to, but not including, hi itself
Input: two numbers, low and hi, and a nonnegative integer, N
"""
return [low + (hi-low)*x for x in fracSteps(N)]
#P3
def dbl_steps(low,hi,N):
"""dbl_steps returns double each of the values in the list that of the steps function
Input: two numbers, low and hi, and a nonnegative integer, N
"""
return [2*x for x in steps(low,hi,N)]
#P4
def fsteps(f,low,hi,N):
"""fsteps returns the f(x) (the outpout of the function) for each of the values in the list of the steps function
Input: function f, two numbers, low and hi, and a nonnegative integer, N
"""
return [f(x) for x in steps(low,hi,N)]
from csplot import *
def sq(x):
""" sq(x) squares its input
input: x, a numeric value
"""
return x**2
from csplot import *
#P5
def finteg(f,low,hi,N):
""" finteg returns an estimate of the definite integral of the function f (the first input) with lower limit low (the second input) and upper limit hi (the third input) where N steps are taken (the fourth input)
finteg simply returns the sum of the areas of rectangles under f, drawn at the left endpoint of the N steps taken from low to hi
"""
return sum(fsteps(f,low,hi,N))*(hi-low)/(N)
def dbl(x):
""" input: a number x (int or float)
output: twice the input
"""
return 2*x
#Q2
def inverse(x):
return (1/x)
def ln(x, N):
finteg (inverse(x),1,x,N)
z = finteg (inverse(x),1,x,N)
return [z]
以下是错误消息:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
ln(42,5)
File "C:\Users\Owner\Documents\EECS 110\Homeworks\hw2pr1.py", line 108, in ln
finteg (inverse(x),1,x,N)
File "C:\Users\Owner\Documents\EECS 110\Homeworks\hw2pr1.py", line 62, in finteg
return sum(fsteps(f,low,hi,N))*(hi-low)/(N)
File "C:\Users\Owner\Documents\EECS 110\Homeworks\hw2pr1.py", line 39, in fsteps
return [f(x) for x in steps(low,hi,N)]
File "C:\Users\Owner\Documents\EECS 110\Homeworks\hw2pr1.py", line 39, in <listcomp>
return [f(x) for x in steps(low,hi,N)]
TypeError: 'float' object is not callable
答案 0 :(得分:2)
致电时
finteg (inverse(x),1,x,N)
你正在调用函数inverse,并将返回值传递给finteg。您需要将反向传递为要评估的函数,如下所示:
finteg (inverse,1,x,N)