0
我想通過函數sqrt(a)找到一個數的平方根。 fixedPoint(f,epsilon)是一個輔助函數。問題是我得到這個TypeError:'浮動'對象不可調用。我是編程新手,所以如果任何人都可以幫助和發現是錯誤,並解釋這個錯誤是什麼意思?爲什麼有這種TypeError?
def fixedPoint(f, epsilon):
"""
f: a function of one argument that returns a float
epsilon: a small float
returns the best guess when that guess is less than epsilon
away from f(guess) or after 100 trials, whichever comes first.
"""
guess = 1.0
for i in range(100):
if abs(f(guess) - guess) < epsilon:
return guess
else:
guess = f(guess)
return guess
def sqrt(a):
def tryit(x):
return 0.5 * (a/x + x)
return fixedPoint(tryit(a), 0.0001)
您正將'tryit' *的返回值作爲參數傳遞給'fixedPoint',**而不是**函數本身。 – jonrsharpe 2014-09-28 12:07:23
'tryit'返回一個浮點數,然後'f'是一個浮點數,'f(guess)'導致錯誤。 – Maroun 2014-09-28 12:07:44