3

我是编程和使用 pydev 在 Eclipse 中运行我的 python 的新手。我正在使用 Eclipse EE 和 Python 2.7.3 我要运行的代码在这里:

def evaluate_poly(poly, x):

        sumPoly = 0

        for i in range(0,len(poly)):
            #calculate each term and add to sum.
            sumPoly = sumPoly+(poly[i]*x**i)
        return sumPoly




def compute_deriv(poly):

    derivTerm = ()

    #create a new tuple by adding a new term each iteration, assign to derivTerm each time.
    for i in range(0,len(poly)):
    #i is the exponent, poly[i] is the coefficient,
    #coefficient of the derivative is the product of the two.
        derivTerm = derivTerm + (poly[i]*i,)
    return derivTerm



def compute_root(poly, x_0, epsilon):

    #define root to make code simpler.
    root = evaluate_poly(poly,x_0)
    iterations = 0

    #until root (evaluate_poly) is within our error range of 0...
    while (root > epsilon) or (root < -epsilon):
    #...apply newton's method, calculate new root, and count iterations.
        x_0 = (x_0 - ((root)/(evaluate_poly(compute_deriv(poly),x_0))))
        root = evaluate_poly(poly,x_0)
        iterations = iterations + 1
    return (x_0,iterations)

print compute_root((4.0,3.0,2.0),0.1,0.0001)

每次我尝试运行这个 Eclipse 时,都会要求我进行 ant build。当我单击确定时,没有任何反应。这只发生在我在函数内部运行函数时,看起来非常基本的代码不是问题。出了什么问题,我该如何解决?

4

1 回答 1

0

如果你只有函数,python 不会做任何事情。它不会无缘无故地运行任意函数。如果你想打印一些东西,你需要调用你的一个函数。最好的方法是将其添加到代码的底部:

if __name__ == '__main__':
    evaluate_poly([1, 2, 3], 4)
于 2012-10-26T21:27:08.440 回答