我已經做了一個簡單的函數稱爲「約」,它將兩個數字相乘,然後將它們除以二。當我單獨使用函數時,它的效果很好,但它似乎代碼豐富,我沒有把它分成兩半,我不知道爲什麼。這是我的代碼錯誤在哪裏,我該如何解決?數字拒絕劃分
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