sqrt()接受2个参数(给定1个)

2024-04-26 03:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

    def sqrt (n, one):
        floating_point_precision = 10*16
        n_float = float(( n * floating_point_precision) // one) / floating_point_precision
        x = (int(floating_point_precision * math.sqrt(n_float)) * one) // floating_point_precision
        n_one = n * one
        while 1:
            x_old = x
            x = ( x + n_one // x) // 2
            if x == x_old:
                return x

print "The newton estimate of", mynum, "is", sqrt(mynum)

Traceback (most recent call last):
  File "/Users/Brett/Desktop/Python/squareroot.py", line 21, in <module>
    print "The newton estimate of", mynum, "is", sqrt(mynum)
TypeError: sqrt() takes exactly 2 arguments (1 given)

Tags: oftheisdefnewtonsqrtfloatone
3条回答

您的sqrt()函数接受两个参数,n和{}。然而,在最后一行中,您只传递了一个参数。在

您的sqrt函数有两个参数,但您只提供了一个参数。第二个参数似乎应该是值“1”。在

print "The newton estimate of", mynum, "is", sqrt(mynum, 1.0)

您已经将sqrt定义为接受两个参数的函数。稍后,您的代码引用您的函数:sqrt。尝试将"/Users/Brett/Desktop/Python/squareroot.py", line 21更改为使用math.sqrt,或者为其提供第二个参数。在

相关问题 更多 >