Python float object does not have len ()

This is the solution to the problem I'm working on (Python training), but even the solution gives me an error.

Here is the code:

def compute_deriv(poly):

    """
    Computes and returns the derivative of a polynomial function. If the
    derivative is 0, returns [0.0].

    Example:
    >>> poly = [-13.39, 0.0, 17.5, 3.0, 1.0]    # - 13.39 + 17.5x^2 + 3x^3 + x^4
    >>> print compute_deriv(poly)        # 35^x + 9x^2 + 4x^3
    [0.0, 35.0, 9.0, 4.0]

    poly: list of numbers, length > 0
    returns: list of numbers
    """

    poly_deriv = []
    if len(poly) < 2:
        return [0.0]
    for j in xrange(1, len(poly)):
        poly_deriv.append(float(j * poly[j]))
    return poly_deriv

This is a solution given to me, but when I use the following code to call a function:

poly1 = (-13.39)
print compute_deriv(poly1)

I get

TypeError: object of type 'float' has no len()

I tried a couple of different things inside the if statement (since this code only breaks when len(poly)is <2;

I tried poly_deriv.append(0.0)and return poly_derivfor example.

+4
source share
2 answers

I suppose there is a typo in the solution, and they assumed that it was a collection of one element.

poly1 = (-13.39,)

or

poly1 = [-13.39]

, , float, , float. (-13.39) -13.39 .

+6

poly1 = (-13.39) . Python , , . :

poly1 = tuple(-13.39)
+3

All Articles