私は学校用のコードで問題に遭遇しています。私はそれを修正する方法を知らない。Pythonでの再帰を伴うニュートンのメソッド
ここに私のコードです。
"""
Convert Newton’s method for approximating square roots in Project 1 to
a recursive function
named newton. (Hint: The estimate of the square
root should be passed as a second
argument to the function.)"""
import math
def newton(x, estimate):
if abs (x-estimate ** 2) <= 0.000001:
return estimate
else:
estimate = newton(x, (estimate + x/estimate)) /2
return estimate
def main():
while True:
x = float(input('Enter a positive number or enter/return key to quit: '))
if x == "":
break
print("Newtons estimate of the sqaure root of ", x, "is: ", newton(x,estimate))
print("The True value of the square root is: ", math.sqrt(x))
main()
コードを強調表示してCtrl + kを押してフォーマットしてください。また、具体的に何が助けを必要としていますか?私はここに質問が表示されません。 – Carcigenicate
これを適切にフォーマットしてください。読みにくいです。 – acushner
「問題に遭遇している」とは、あなたが私たちに伝えたいことすべてです。問題を秘密にしておけば、どうすれば助けてくれるのでしょうか? –