Попытка убить двух зайцев одним выстрелом Я решил написать немного кода, который позволил бы мне практиковать питон и исчисление одновременно. У меня есть два отдельных файла: 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
И текст из 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)
Ошибка:
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
мне нужно объявить input_function в Производная ли .py? Я бы подумал, что объявить его в newton_method.py было бы достаточно.