我如何将这段代码放入函数中

时间:2019-02-13 22:54:49

标签: python python-3.x

我想将其放入函数newton(x)中,该函数允许用户在其中输入x的值。

from math import *
x=20
for iteration in range(1, 1001):

        xnew = x - (((3*10**-5)*exp((log1p(10**3/(3*10**-5)))*(1-exp(-.12*x))))-1)/(.12*(3*10**-5)*exp((log1p(10**3/(3*10**-5))))*(1-exp(-.12*x)*(-.12*x))) # the Newton-Raphson's formula
        print("Error",x-xnew," Xn:   ", xnew)
        if abs(xnew - x) < 0.000001:
            break  

        x = xnew  
print('The root : %0.5f' % xnew)
print('The number of iterations : %d' % iteration)

1 个答案:

答案 0 :(得分:2)

您可以使其成为返回2元组的函数:

ngOnDestroy() {
    this.componentActive = false;
}