def fvals_sqrt(x):
"""
Return f(x) and f'(x) for applying Newton to find a square root.
"""
f = x**2 - 4.
fp = 2.*x
return f, fp
def solve(fvals_sqrt, x0, debug_solve=True):
"""
Solves the sqrt function, using newtons methon.
"""
fvals_sqrt(x0)
x0 = x0 + (f/fp)
print x0
當我嘗試調用函數解決,蟒蛇回報Python變量範圍:與函數作爲參數
NameError: global name 'f' is not defined
顯然,這是一個範圍的問題,但我怎麼能使用f內我的解決功能?