그래서 학교용 코드 문제가 있습니다. 나는 그것을 고치는 법을 모른다.파이썬에서 재귀를 사용하는 Newton의 메서드
여기 내 코드입니다.
"""
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
"문제가 발생했습니다"라는 말은 기꺼이 알려주는 것이 전부입니까? 문제를 비밀로한다면 어떻게 도와야합니까? –