Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算平方根的逻辑思考_Python_Python 3.3_Square Root - Fatal编程技术网

Python 计算平方根的逻辑思考

Python 计算平方根的逻辑思考,python,python-3.3,square-root,Python,Python 3.3,Square Root,在过去的一个小时里,我一直在试图破解这个问题,但在这里遇到了一些麻烦。这就是问题所在 此计算数字n的平方根的方法从 对平方根进行(非零)猜测。然后使用 原始猜测根据公式计算新猜测 newGuess = ((n / oldGuess) + oldGuess) / 2.0; 有两个变量oldGuess和newGuess。将oldGuess初始化为 n/2.0并根据上述公式计算newGuess。使用 一个while循环,只要 oldGuess和newGuess之间的差异大于 1.0E-06。不要忘记

在过去的一个小时里,我一直在试图破解这个问题,但在这里遇到了一些麻烦。这就是问题所在

此计算数字n的平方根的方法从 对平方根进行(非零)猜测。然后使用 原始猜测根据公式计算新猜测

newGuess = ((n / oldGuess) + oldGuess) / 2.0;
有两个变量
oldGuess
newGuess
。将
oldGuess
初始化为
n/2.0
并根据上述公式计算
newGuess
。使用 一个while循环,只要
oldGuess
newGuess
之间的差异大于
1.0E-06
。不要忘记将
oldGuess
的值重置为
newGuess
while循环中的值

在程序中,您将提示用户输入正数。 如果数字为负数,则打印错误消息并要求用户 再试一次。对于正数,使用 以上方法。找出你得到的平方根之间的差值 以及使用求幂运算符获得的值。写 输出用户输入的值、计算的平方根和 差异(你的平方根-
n**0.5

这是我目前的节目

def main():
    n = eval(input("Enter a positive number: "))
    while (n <= 0):
        print ("Error please re-input")
        n = eval(input("Enter a positive number: "))
     
    oldGuess = n / 2.0
    newGuess = ((n / oldGuess) + oldGuess) / 2.0;
    difference = n - n ** 0.5      
    while (difference < 1 * 10 ** -6):
        print ("Error")
        difference = abs(n - n ** 0.5)
    print ("Difference:", difference)

main()
def main():
n=eval(输入(“输入正数:”)
while(n
while True:
n=浮点(输入(“输入正数:”)
如果n>0:
打破
打印(“错误,请重新输入”)
oldGuess=n/2.0
尽管如此:
newGuess=((n/oldGuess)+oldGuess)/2.0;
旧猜=新猜
如果-1e-6
将那些
eval()
s更改为
float()
s。
eval()
执行它所传递的任何代码,这意味着用户可能会在那里键入恶意代码

现在,将其用于第二部分:

oldGuess = n / 2.0
newGuess = ((n / oldGuess) + oldGuess) / 2.0
while (abs(oldGuess - newGuess) > 1e-06):
    oldGuess, newGuess = newGuess, ((n / oldGuess) + oldGuess) / 2.0
print("Guess: " + str(n))
print("My root: " + str(newGuess))
print("Accuracy: " + str(newGuess - (n**0.5)))
逗号语法是一种Python习语,用于交换值,而无需执行以下操作:

temp = new
new = old * something
old = temp
while循环的条件是当差值小于该值(非常小)时结束循环。因此,只要差值大于该值,就将循环

注意:您也可以使用
math.sqrt(n)
而不是
n**0.5
。您必须
导入数学

如果要查看程序正在执行的操作,请尝试在
while
循环中打印
oldGuess
newGuess
的值。您将看到它正在更改这些值,直到找到答案为止

编辑

我注意到您似乎被为什么必须执行
oldGuess=newGuess
的操作绊倒了。让我解释一下:
=
操作符与数学中的等号不同。等号表示左侧的东西与右侧的东西是相同的;也就是说,它们是等价的。在Python中,
=
操作符或者说“给左边的东西和右边的东西相同的值。”这被称为赋值操作符。你想到的是
=
操作符,它测试等价性(基本上)


如您所见,当您使用
=
操作符时,您不会“链接”这些变量放在一起,表示它们现在是相同的东西。如果设置
b=a
,然后设置
b=c
b==a
将变为false,因为
b
不再具有与
a
相同的值。
a
也不会改变,因为
b
已被赋值,而不是以其他方式赋值und.想想
=
操作符看起来像
,所以我想出来了,谢谢大家的帮助。我不知道我们不能在这里发布家庭作业问题,但我正在努力明确地学习如何编码,以便我能做得更好。这是我的最终解决方案

def main():

    n = float(input("Enter a positive number: "))
    while (n <= 0):
     print ("Error please re-input")
     n = eval(input("Enter a positive number: "))

    oldGuess = n / 2.0
    newGuess = 0

    difference = 10
    while (difference >= 1 * 10 ** -6):
     newGuess = ((n / oldGuess) + oldGuess) / 2.0
     difference = abs(newGuess - oldGuess)
     oldGuess = newGuess

    print ("Square Root is: ", newGuess)


    differenceSqrt = newGuess - n ** 0.5
    print ("Difference is: ", differenceSqrt)



main()
def main():
n=浮点(输入(“输入正数:”)
而(n=1*10**-6):
newGuess=((n/oldGuess)+oldGuess)/2.0
差值=绝对值(新猜测-旧猜测)
旧猜=新猜
打印(“平方根为:”,newGuess)
差分qrt=newGuess-n**0.5
打印(“差异为:”,差异为QRT)
main()

我仍然不知道如何有效地使用中断,所以非常感谢gnibbler,但是我不能很好地理解您的代码。(很抱歉,这是一个新手)

这是一个家庭作业问题吗?当绝对差值大于一个小数值而不是小于一个小数值时,你想继续迭代。使用
eval
是危险的。使用
float
代替你试图实现的平方根算法,
n**0.5
确实不属于这里。如果你不确定你的循环是什么这样做,要么添加显示给您的
print
调用,要么使用内置调试器、调试器周围您最喜欢的IDE图形包装器等,然后找出答案。这比猜测或试图推理容易得多。@user2357112,您应该将-1保存为错误答案。关于家庭作业的SO策略非常明确。此外,没有当我发布这篇文章时提到了家庭作业。@gnibbler很明显这是家庭作业,但你是对的。所以我不知道中断的确切含义。我在这里试图遵循你的逻辑。我仍然不明白为什么我们需要一个oldGuess=newGuess?我们不能只做一个if语句,就像如果newGuess-oldGuess=1e-6,然后返回吗没有其他内容:打印差异?@SulimanSharif,
中断是为了避免在进入循环之前初始化
newGuess
。@SulimanSharif:你不想什么都不返回。你想不断做出更好的猜测,直到你足够接近为止,然后返回你的最佳猜测。如果你不更新
oldGuess
,你只需要继续使用你的第一个猜测,并继续重新计算你的第二个猜测。谢谢你的帮助。肯定需要澄清
>>> a = 10
>>> b = 4
>>> b = a
>>> b
10
>>> a == b
True
>>> c = 6
>>> b = c
>>> b
6
>>> a == b
False
>>> b == c
True
>>> a == c
False
>>> a,b,c
(10, 6, 6)
def main():

    n = float(input("Enter a positive number: "))
    while (n <= 0):
     print ("Error please re-input")
     n = eval(input("Enter a positive number: "))

    oldGuess = n / 2.0
    newGuess = 0

    difference = 10
    while (difference >= 1 * 10 ** -6):
     newGuess = ((n / oldGuess) + oldGuess) / 2.0
     difference = abs(newGuess - oldGuess)
     oldGuess = newGuess

    print ("Square Root is: ", newGuess)


    differenceSqrt = newGuess - n ** 0.5
    print ("Difference is: ", differenceSqrt)



main()