Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 导入函数时出现全局名称错误_Python_Python 2.7 - Fatal编程技术网

Python 导入函数时出现全局名称错误

Python 导入函数时出现全局名称错误,python,python-2.7,Python,Python 2.7,为了一举两得,我决定写一些代码,让我可以同时练习python和微积分。我有两个独立的文件,Derivative.py和newton_method.py(我知道我应该更好地正确命名我的文件)。来自Derivatibe.py的文本如下 def fx(value, function): x = value return eval(function) def function_input(input): function = str(input) return func

为了一举两得,我决定写一些代码,让我可以同时练习python和微积分。我有两个独立的文件,Derivative.py和newton_method.py(我知道我应该更好地正确命名我的文件)。来自Derivatibe.py的文本如下

def fx(value, function):
    x = value
    return eval(function)

def function_input(input):
    function = str(input)
    return function

def derivative_formula(x, h):
    return (fx(x - h, input_function) - fx(x + h, input_function)) / (2.0 * h)

def derivative_of_x(x):
    error = 0.0001
    h = 0.1
    V = derivative_formula(x, h)
    print V
    h = h / 2.0
    derivative_estimate = derivative_formula(x, h)
    while abs(derivative_estimate - V) < error:
        V = derivative_formula(x, h)
        h = h / 2.0
        derivative_estimate = derivative_formula(x, h)
        print derivative_estimate
    return derivative_estimate
def fx(值、函数):
x=值
返回评估(功能)
def功能_输入(输入):
函数=str(输入)
返回函数
def导数_公式(x,h):
返回(fx(x-h,输入函数)-fx(x+h,输入函数))/(2.0*h)
_x(x)的def导数_:
误差=0.0001
h=0.1
V=导数_公式(x,h)
印刷品
h=h/2.0
导数估计=导数公式(x,h)
而abs(导数估计-V)<误差:
V=导数_公式(x,h)
h=h/2.0
导数估计=导数公式(x,h)
打印导数估计值
收益导数估计
来自newton_method.py的文本是:

from Derivative import *

input_function = function_input(raw_input('enter a function with correct python syntax'))

E = 1 * (10 ** -10)

guessx = float(raw_input('Enter an estimate'))

def newton_method(guessx, E, function):
    x1 = guessx
    x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
    while x2 - x1 < E:
        x1 = x2
        x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
    return x2

print "The root of that function is %f" % newton_method(guessx, E, input_function)
来自衍生工具导入的
*
输入\函数=函数\输入(原始\输入(“输入具有正确python语法的函数”))
E=1*(10**-10)
猜测x=浮动(原始输入(“输入估计值”)
定义牛顿法(猜测x、E、函数):
x1=x
x2=x1-(fx(x1,输入函数)/x(x1)的导数)
当x2-x1
错误:

Traceback (most recent call last):
  File "newton_method.py", line 17, in <module>
    print "The root of that function is %f" % newton_method(guessx, E,         input_function)
  File "newton_method.py", line 11, in newton_method
    x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
  File "C:\Users\159micarn\Desktop\Python\Derivative.py", line 15, in derivative_of_x
    V = derivative_formula(x, h)
  File "C:\Users\159micarn\Desktop\Python\Derivative.py", line 10, in derivative_formula
    return (fx(x - h, input_function) - fx(x + h, input_function)) / (2.0 * h)
NameError: global name 'input_function' is not defined
回溯(最近一次呼叫最后一次):
文件“newton_method.py”,第17行,在
打印“该函数的根是%f”%newton\u方法(猜测x,E,输入函数)
文件“newton_method.py”,第11行,在newton_method中
x2=x1-(fx(x1,输入函数)/x(x1)的导数)
文件“C:\Users\159micarn\Desktop\Python\Derivative.py”,第15行,在\u x的导数\u中
V=导数_公式(x,h)
文件“C:\Users\159micarn\Desktop\Python\Derivative.py”,第10行,在导数公式中
返回(fx(x-h,输入函数)-fx(x+h,输入函数))/(2.0*h)
NameError:未定义全局名称“输入函数”

我需要在Derivative.py中声明输入函数吗?我本以为用newton_method.py声明就足够了。

模块无法访问导入它们的模块的名称空间。想象10个模块导入
Derivative.py
。它将使用哪个
input\u函数
input_函数
只是一个和另一个值,应该是
导数_公式
的附加输入参数