x is not defined, and you cannot pass f(x) in the parameter. Try doing something like this:
def f(x):
return x**2 + x - 5
def derivative(f,x,h):
return 1/(2*h) * (f(x+h) - f(x-h))
x=12345
print derivative(f,x,6)
Answer from ergonaut on Stack Overflowx is not defined, and you cannot pass f(x) in the parameter. Try doing something like this:
def f(x):
return x**2 + x - 5
def derivative(f,x,h):
return 1/(2*h) * (f(x+h) - f(x-h))
x=12345
print derivative(f,x,6)
When you tell it to :
print derivative(f(x),4,6)
you haven't defined the x you are passing as a parameter in f(x).
You can do that like so, for example with x = 1 :
def f(x):
return x**2 + x - 5
def derivative(f,x,h):
return 1/(2*h) * (f(x+h) - f(x-h))
x=1
print derivative(f(x),4,6)
Hi all,
I am here today to learn about the mistake I made on a programming test. I kept getting this error and I did not know how to fix it. Sorry if my code is bad, I am really new to coding and I hope to pursue it in the future. Currently when I run this block of code, I get this error. " Error: NameError: name 'x' is not defined ". Is there any way to fix this? If so, please help me. Thanks in advance.
P.S. ( The def CalcCircle is suppose to be indented like this)
import math
class Point:
#Define the class here
def __init__(self, initX, initY):
self.initX = x
self.initY = y
def getX(self):
return self.x
def getY(self):
return self.y
def get_line_to(self, point):
return ((point.y - self.y) / (point.x - self.x), self.y - self.x * (point.y-self.y) / (point.x - self.x))
def distanceFromPoint(self, P):
distacex = (P.getX() - self.x)
distancey = (P.getY() - self.y)
return math.sqrt(distancex ** 2 + distancey ** 2)
def halfway(self, point):
return Point((self.x + point.x) / 2, (self.y + point.y) / 2)
def calcCircle(P, Q, R):
''' P, Q, R and three Point objects '''
#Define the function here
halfx = P.halfway(Q)
gltx = P.get_line_to(Q)
halfy = R.halfway(Q)
glty = R.get_line_to(Q)
a = -1/gltx[0]
b = -1/glty[0]
c = halfx.getY() - a * halfx.getX()
d = halfy.getY() - b * halfx.getX()
o = Point((d - c) / (a - b), (a * d - b * c) / (a - b))
r = o.distanceFromPoint(Q)
return (o , r)
"NameError: name is not defined"... but it is?
list - NameError: name 'x' is not defined Python - Stack Overflow
NameError: name 'x' is not defined in jupyer notebook upon using %%time
How to solve the Python error "NameError: name 'X' is not defined" - Stack Overflow
How to solve undefined variable in python
What causes undefined variable?
When doing global price_buy that means you use the globally defined price_buy to be defined localy in your method but
- neither
price_buynornumber_exitare defined globally (outside a method) - you don't need global variable
They only to be local, and just better : inlined
def fun_price_buy():
price_buy = np.random.randint(50000,300000)
return price_buy
# inline, no temporaty variable is needed
def fun_price_buy():
return np.random.randint(50000,300000)
Finally, and if you want to get the value from the method in a variable for doing something with it:
import numpy as np
# This function generates a list of numbers under certain rules
def num_var_list(number_exit):
number_list = []
number_target = number_exit
num_max_robot = (number_target * 20) / 100
while num_max_robot > 0:
num_robot = np.random.randint(1,int(num_max_robot))
if num_robot > number_target:
number_list.append(number_target)
else:
number_list.append(num_robot)
number_target = number_target - number_target
return number_list
def fun_price_buy():
return np.random.randint(50000,300000)
def fun_mx_buy():
return np.random.randint(50, 150)
lista_number_list = []
lista_price_buy = []
lista_mx_buy = []
# This loop append each function 50 times to a new list
while len(lista_price_buy) <= 50:
number_exit = fun_mx_buy()
price_buy = fun_price_buy()
vr_list = num_var_list(number_exit)
lista_number_list.append(vr_list)
lista_price_buy.append(price_buy )
lista_mx_buy.append(number_exit )
why do you have so much global ?
def num_var_list():
number_exit=fun_mx_buy()
number_list = []
number_target = number_exit
num_max_robot = (number_target * 20) / 100
while num_max_robot > 0:
num_robot = np.random.randint(1, int(num_max_robot))
if num_robot > number_target:
number_list.append(number_target)
else:
number_list.append(num_robot)
number_target = number_target - number_target
return number_list
is it works for you?
Global in the context you put does not work across modules... if you did something like this, however, it would work
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print globvar # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
see more here
The variable x doesn't be global in other modules, if you initial it as global in your function ,the global keyword makes a variable global inside its module that been loaded . So you need to simply pass your variable as argument to function and then return the result :
def increment(x):
x += 1
return x
And in the main code , you can call the function as following :
x=increment(x)