我正在尝试编写一个函数来计算牛顿法。希望我的代码中不断出现错误。这是给我写代码的提示
这是我写下的代码
import math
def newton(x):
tolerance = 0.000001
estimate = 1.0
while True:
estimate = (estimate + x / estimate) / 2
difference = abs(x - estimate ** 2)
if difference <= tolerance:
break
return estimate
def main():
while True:
x = input("Enter a positive number or enter/return to quit: ")
if x == '':
break
x = float(x)
print("The program's estimate is", newton(x))
print("Python's estimate is ", math.sqrt(x))
main()
它似乎正在工作,但是当我对 Cengage 运行检查时,我不断收到此错误
我不太确定这意味着什么,因为我的代码似乎运行得很好。谁能帮忙解释一下?
元芳怎么了
倚天杖
相关分类