类型错误"浮点"对象不可调用



我知道这个错误通常是因为您混淆了一个函数和一个名称,有点像问题。我好像想不通。

#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:UsersOwnerDocumentsEECS 110Homeworkshw2pr1.py", line 108, in ln
    finteg (inverse(x),1,x,N)
  File "C:UsersOwnerDocumentsEECS 110Homeworkshw2pr1.py", line 62, in finteg
    return sum(fsteps(f,low,hi,N))*(hi-low)/(N)
  File "C:UsersOwnerDocumentsEECS 110Homeworkshw2pr1.py", line 39, in fsteps
    return [f(x) for x in steps(low,hi,N)]
  File "C:UsersOwnerDocumentsEECS 110Homeworkshw2pr1.py", line 39, in <listcomp>
    return [f(x) for x in steps(low,hi,N)]
TypeError: 'float' object is not callable

当您调用时

finteg (inverse(x),1,x,N)

您正在调用inverse函数,并将返回值传递给finteg。您只需要将inverse作为要评估的函数,如下所示:

finteg (inverse,1,x,N)

相关内容

  • 没有找到相关文章

最新更新