Domanda

I know this error is usually because you are mixing up a function and a name, sort of issue. I can't seem to figure it out.

#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]

and here is the error message:

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
È stato utile?

Soluzione

When you call

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

you are calling the function inverse, and passing the return value to finteg. You need to just pass inverse as the function to be evaluated, like so:

finteg (inverse,1,x,N)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top