修正牛顿法编程python_编程牛顿法python3.6

对于我的作业,我们被要求创建一个程序,在其中计算牛顿法。有很多技术我们还没有学会,这就是为什么我只限于如何设计我的代码。在

我特别需要帮助,使实际的牛顿法循环通过输入我使用的迭代次数,但任何建议都会很乐意接受。在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()

今晚就要交了…我之前误解了作业,所以我以为我已经搞定了。请帮忙!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值