为什么python闭包不起作用?

为什么python闭包不起作用?,python,closures,nested-function,Python,Closures,Nested Function,尝试获取工作函数求值_polypoly,x_n和compute_deriv2poly,x_n以在函数compute_rootpoly,x_n,epsilon中工作 代码运行时,不会抛出任何错误,但不会返回任何内容。 这是密码 # 6.00 Problem Set 2 # # Successive Approximation # def evaluate_poly(poly, x_n): """ Computes the polynomial function for a giv

尝试获取工作函数求值_polypoly,x_n和compute_deriv2poly,x_n以在函数compute_rootpoly,x_n,epsilon中工作

代码运行时,不会抛出任何错误,但不会返回任何内容。 这是密码

# 6.00 Problem Set 2
#
# Successive Approximation
#

def evaluate_poly(poly, x_n):
    """
    Computes the polynomial function for a given value x. Returns that value.

    Example:
    >>> poly = (0.0, 0.0, 5.0, 9.3, 7.0)    # f(x) = 7x^4 + 9.3x^3 + 5x^2
    >>> x = -13
    >>> print evaluate_poly(poly, x)  # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2
    180339.9

    poly: tuple of numbers, length > 0
    x: number
    returns: float
    """
    valHolder=0
    for i in range((0),(len(poly))):
        valHolder=valHolder+(poly[i])*(x_n** i)
    return float(valHolder)

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)    # x^4 + 3x^3 + 17.5x^2 - 13.39
    >>> print compute_deriv(poly)        # 4x^3 + 9x^2 + 35^x
    (0.0, 35.0, 9.0, 4.0)

    poly: tuple of numbers, length > 0
    returns: tuple of numbers
    """
    newList=list(poly)
    for i in range((0),(len(poly))):
        ##if i > 0.0:
        a=newList[i]
        b=i

        if b == 0:
            continue
        else:
            derivedNumber=a*b
        newList.append(derivedNumber)

    finishedTuple=tuple(newList)
    return finishedTuple[len(poly):]
    print "FINISHED TUPLE IS= ",finishedTuple

def compute_deriv2(poly,x_n):
    """
    Similar to first compute_deriv, this time takes a value for x

    """
    newList=list(poly)
    for i in range((0),(len(poly))):
        ##if i > 0.0:
        a=newList[i]
        b=i

        if b == 0:
            continue
        else:
            derivedNumber=a*b
        newList.append(derivedNumber)

    finishedTuple=tuple(newList[len(poly):])
    derivedValue=0
    for g in range((0),len(finishedTuple)):
        c=finishedTuple[g]
        d=g
        derivedValue+=c*(x_n**d)
    return derivedValue




def compute_root(poly, x_n, epsilon):
    """
    Uses Newton's method to find and return a root of a polynomial function.
    Returns a tuple containing the root and the number of iterations required
    to get to the root.

    Example:
    >>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0)    #x^4 + 3x^3 + 17.5x^2 - 13.39
    >>> x_0 = 0.1
    >>> epsilon = .0001
    >>> print compute_root(poly, x_0, epsilon)
    (0.80679075379635201, 8.0)

    poly: tuple of numbers, length > 1.
         Represents a polynomial function containing at least one real root.
         The derivative of this polynomial function at x_0 is not 0.
    x_0: float
    epsilon: float > 0
    returns: tuple (float, int)
    """
    evaluate_poly(poly,x_n)
    compute_deriv2(poly,x_n)
    newList=list(poly)
这是我使用的输入

>>>>compute_root((-13.39,0.0,17.5,3.0,1.0),0.1,0.0001)

当我自己调用它们时,所有单独的函数都能工作,除了compute_根函数。你知道我该如何关闭它才能工作吗?我必须在compute_root函数中再次定义它们吗

计算根中没有返回值

    e = evaluate_poly(poly,x_n)
    c = compute_deriv2(poly,x_n)
    newList=list(poly)
    return e, c # or whatever you want

是的,不会抛出任何错误,但在compute_root中不会返回任何内容。这意味着这些函数的返回值:

evaluate_poly(poly,x_n)
compute_deriv2(poly,x_n)
这些都被忽略了。此外,由于默认情况下不返回任何内容的函数不返回任何内容,请执行以下操作:

print compute_root(...)
将不可避免地不打印任何内容

要解决此问题,请在compute_root中添加一个return语句,以返回所需的内容。我不知道这到底是什么,但可以举一个例子:

a = evaluate_poly(poly,x_n)
b = compute_deriv2(poly,x_n)
c = newList=list(poly)
return a, b, c

你不应该用compute\u root返回一些东西吗?可能什么都不返回,因为compute\u root没有返回语句…实际上,没有返回应该是正确的答案。这与闭包有什么关系?这里没有闭包。酷。确保选择其中一个答案作为您的接受答案。