【发布时间】:2018-04-09 07:58:09
【问题描述】:
所以我的学校代码遇到了问题。我不知道如何解决它。
这是我的代码。
"""
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。另外,您具体需要什么帮助?我在这里没有看到任何问题。
-
请正确格式化,很难阅读。
-
“遇到问题”是您愿意告诉我们的一切吗?如果您对问题保密,我们应该如何提供帮助?
-
你没有给函数一个估计的初始值。
-
您好 Heather,欢迎来到 StackOverflow。我不会因任何反对票而气馁,它们在第一个问题中很常见。你应该看看help center 和How to Ask。理想情况下,您的问题应该包括minimal reproducible example,您的问题大多如此!我建议的最大改进是明确说明您遇到的问题。您期望的输出是什么,它与您当前获得的输出有何不同?你有错误吗?如果是这样,您应该发布完整的错误消息,包括堆栈跟踪。 “这行不通”在这里往往得不到很好的接受。
标签: python python-3.x recursion newtons-method