我已经做了一个简单的函数称为“约”,它将两个数字相乘,然后将它们除以二。当我单独使用函数时,它的效果很好,但它似乎代码丰富,我没有把它分成两半,我不知道为什么。这是我的代码错误在哪里,我该如何解决?数字拒绝划分
import math
def Approx(low,high):
base = low * high
return base/2
root = float(input("What to approx the sqrt of : "))
vague = float(input("How off can it be? : "))
wrong = True
oroot = root
root = math.floor(float(root))
trunk = root + 1
step = 0
while wrong:
if Approx(root,trunk) > oroot - vague and Approx(root,trunk) < oroot:
print("Done. " + str(step) + " steps taken.")
else:
if Approx(root,trunk) > oroot:
temproot = root
root = Approx(root,trunk)
trunk = temproot
step += 1
print("Step " + str(step) + " finished. Approx is " + str(Approx(root,trunk)))
else:
temptrunk = trunk
trunk = Approx(root,trunk)
root = trunk
step += 1
print("Step " + str(step) + " finished. Approx is " + str(Approx(root,trunk)))
if step > 50:
print("More than fifty steps required.")
wrong = False
是什么让你觉得师没有发生? – user2357112
如果'Approx(root,trunk)> oroot - 模糊和Approx(root,trunk)(oroot - 模糊)和(Approx(root,干线)
albert
'Approx'的期望输出是什么?回想一下Python 3中'5 // 2'('5 // 2 = 2')和'5/2'('5/2 = 2.5')的区别。而在Python 2中则相反。 – albert