Python:我输入的代码没有任何作用

2024-05-17 17:11:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python执行二分法,当我尝试运行它时,什么都没有发生。 以下是我使用的代码:

import math
a=0
b=1    
def f(x):    
    return math.sqrt(x)-math.cos(x)
while b-a>0.0001:    
    c=(a+b)/2
    if f(a)*f(c)>0:        
        a=c
else:
        b=c
print(c)

Tags: 代码importreturnifdefmathsqrtcos