对于我的作业,我们被要求创建一个程序,在其中计算牛顿法。有很多技术我们还没有学会,这就是为什么我只限于如何设计我的代码。在
我特别需要帮助,使实际的牛顿法循环通过输入我使用的迭代次数,但任何建议都会很乐意接受。在def main():
print("This program computes square roots through the Newton method")
x = eval(input("Enter a number to calculate the square root of: "))
r = eval(input("How many iterations should I use?: "))
for i in range(1, r+1):
guess = x/2
for n in (1, r+1):
n = (guess + (x/guess))/2 # Newton's Method formula
import math
y = math.sqrt(x) # actual square root value
for i in range(1, r+1):
print(i, n, n-y) # attempt at extra credit
print("My guess for the square root of", x, "is", n)
print("The difference between my guess and the actual result is:", n-y)
main()
今晚就要交了…我之前误解了作业,所以我以为我已经搞定了。请帮忙!在