为什么它没有显示任何结果,但没有显示错误

why doesn't it shows no result,but shows no error

我写了一个代码来获取 function.But 它没有显示任何 result.neither 是否显示任何错误:

import math
from math import e
#finding root of f(x)=(e**-x)-x:
#f'(x)=(-e**-x)-1
#y=x-(f(x)/f'(x))
def f(x):
    x=0
    y=x-(((e**-x)-x)/((-e**-x)-1))
    z=((y-x)/y)*100
    while z<(10**-8):
        print(f"The root is {y}")
        x=y
    

z的初始值为100。

因此不会满足z < (10**-8),不会进入while循环

但是,我们可以使用递归来找出所需的值:x = 0.5671432904,而我们试图找到我们需要设置一个 maximum-iteration 值,否则过程将以错误结束。

此外,由于我们要使用递归,我们不再需要 while 循环,而是可以使用 if 语句进行检查。

这是我的解决方案:

from math import e
# desired output: x=0.5671432904
max_iter = 950
count = 0
result = -1


def f(x):
    global count, result
    count += 1
    if count < max_iter:
        y = x-(((e**-x)-x)/((-e**-x)-1))
        z = ((y-x)/y)*100
        if z < (10**8):
            x = y
            result = x
            f(x)


f(0)
print("The root is {:.10f}".format(result))

输出:

The root is 0.5671432904
from math import e


def f():
    x = 0
    y = x-(((e**-x)-x)/((-e**-x)-1))
    z = ((y-x)/y)*100
    print(z)
    while z < (10**-8):
        print("The root is {}".format(y))
        x = y

f()

Z 的输出为 100

大于 (10**-8) 所以 while 循环永远不会工作