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
当我尝试调用函数solve时,python返回:
NameError: global name 'f' is not defined
显然这是一个范围问题,但是如何在我的求解函数中使用 f 呢?