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 Overflow
🌐
Reddit
reddit.com › r/learnpython › help with error: nameerror: name 'x' is not defined
r/learnpython on Reddit: Help with Error: NameError: name 'x' is not defined
January 30, 2019 -

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)

Discussions

"NameError: name is not defined"... but it is?
So, im making a tic-tac-toe game and im nearly finished i think. im trying to make it so the game can be played more than just once before it ends. So i put it into a def() function… but running it results in NameError: … More on discuss.python.org
🌐 discuss.python.org
9
0
October 27, 2021
list - NameError: name 'x' is not defined Python - Stack Overflow
I'm trying to get around this error but can't find a solution. Actually the code worked perfectly some time but suddendly started to drop the error: "NameError: name 'number_exit' is not defined& More on stackoverflow.com
🌐 stackoverflow.com
NameError: name 'x' is not defined in jupyer notebook upon using %%time
A very strange error occurred with me today, I was trying running my jupyter notebook until one of my cell contained a code segment from a package named featuretools which uses dask, distributed an... More on github.com
🌐 github.com
9
April 14, 2019
How to solve the Python error "NameError: name 'X' is not defined" - Stack Overflow
This is the error : Traceback (most recent call last): File "test.py", line 27, in id_check(id,gender) NameError: name 'gender' is not defined And this is my simple ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to solve undefined variable in python
To solve the NameError: name 'x' is not defined error in Python, you need to make sure that the variable is properly defined and assigned a value before it is used. The variable should also be referenced correctly, with the correct case and spelling.
🌐
rollbar.com
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
What causes undefined variable?
In Python, a variable is not created until a value is assigned to it. If an attempt is made to use a variable before it is defined, a NameError: name 'x' is not defined error is thrown. The error message typically includes the name of the variable that is causing the problem and the line of code where the error occurred.
🌐
rollbar.com
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
🌐
Rollbar
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
In Python, a NameError: name 'x' is not defined error is raised when the program attempts to access or use a variable that has not been defined or assigned a value.
Published: May 16, 2023
🌐
Bobby Hadz
bobbyhadz.com › blog › python-nameerror-name-is-not-defined
NameError: name 'X' is not defined in Python [Solved] | bobbyhadz
April 8, 2024 - The greet function expects to get called with a string but we forgot to wrap the string in quotes, so the name 'X' is not defined error occurred. This can also happen when passing a string to the print() function without wrapping the string ...
🌐
Its Linux FOSS
itslinuxfoss.com › home › python › nameerror: name ‘x’ is not defined in python
NameError: name 'X' is not defined in Python
November 17, 2022 - The “NameError: name ‘X’ is not defined” occurs in Python when the operated variable or function is not defined or accessed before defining it in the program
🌐
OpenPython
openpython.org › home › articles › fix python nameerror: 'name is not defined' (step-by-step)
Fix Python NameError: 'Name Is Not Defined' (Step-by-Step) | OpenPython
May 30, 2026 - This is actually a Python 2-to-3 compatibility change that catches developers who learned on Python 2. In Python 2, the comprehension variable would leak into the enclosing scope (which was itself surprising and error-prone). Python 3 fixed this by giving comprehensions their own scope, which is cleaner but breaks code that relied on the leaking behaviour. x = 10 del x print(x) # NameError: 'x' is not defined
Find elsewhere
🌐
Python Forum
python-forum.io › thread-35295.html
NameError: name “x” is not defined ... even though x is defined
October 17, 2021 - I keep getting this name error message when I just want to print the value of x. The picture clearly shows that I am defining x [x = 5]. I have uninstalled and reinstalled both Python 3.10 and Visual Studio Code several times but the error persists....
🌐
Python.org
discuss.python.org › python help
"NameError: name is not defined"... but it is? - Python Help - Discussions on Python.org
October 27, 2021 - So, im making a tic-tac-toe game and im nearly finished i think. im trying to make it so the game can be played more than just once before it ends. So i put it into a def() function… but running it results in NameError: name 'turn' is not defined. Any ideas why and how to fix this? def game(): blanksheet = "|1|2|3|\n|4|5|6|\n|7|8|9|" sheet = blanksheet print(sheet) gone = [] turn = 0 def play(XO,player,listXO): global turn global sheet global listx global listo ...
Top answer
1 of 5
3

When doing global price_buy that means you use the globally defined price_buy to be defined localy in your method but

  • neither price_buy nor number_exit are 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 )
2 of 5
2

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?

🌐
PyBlog
pyblog.in › home › nameerror: name ‘x’ is not defined — python error causes and fix
NameError: name 'x' is not defined — Fix It Fast
May 12, 2026 - NameError: name 'x' is not defined ... referenced. The most common causes are simple: a typo, a missing import, a function that returns None, or accidentally overwriting a built-in....
🌐
Codecademy
codecademy.com › forum_questions › 52d9870a52f8638f4100043e
NameError: name 'x' is not defined | Codecademy
I was programming on python when i came across this error, this is my code: x=x+1 · mc.setBlock(xyz.x,xyz.y-1,xyz.z,block.WOOL.id, x) and this is the error: NameError:name ‘x’ is not defined · Please help as i am new to python :) 0 votes · Permalink · you’re defining x as x + 1, so what is x before that?
🌐
GitHub
github.com › jupyter › notebook › issues › 4566
NameError: name 'x' is not defined in jupyer notebook upon using %%time · Issue #4566 · jupyter/notebook
April 14, 2019 - Everything else is defined but the variables in this cell! Only to find that after removing %%time from this cell, it worked! Is there some relation to dask or any other package that magic constants cause problem? I am using below OS · NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/" Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) [GCC 7.3.0] on linux jupyter.__version__ = 1.0.0 IPython.__version__ = 7.4.0 ·
Author: jupyter
🌐
Stack Overflow
stackoverflow.com › questions › 79749856 › python-why-am-i-getting-nameerror-name-x-is-not-defined
Python : why am I getting " nameError: name ' x ' is not defined? " - Stack Overflow
I tried assigning x = 5 and then printing it. I expected the program to print 5. But instead, I got an error: "NameError: name 'y' is not defined".
🌐
Stack Overflow
stackoverflow.com › questions › 73736270 › nameerror-name-x-is-not-defined-but-it-is
python - NameError: name 'X' is not defined but it is - Stack Overflow
Method get_soup_from_url is from another "utils" existing python library. The simplest way is to check your utils into something else and unique. If that fixes your problem, try using relative imports to ensure you are importing correct "utils".
🌐
Medium
medium.com › @chifahatim1 › how-to-fix-nameerror-name-is-not-defined-in-python-beginner-guide-2026-5f4a42dae9dd
How to Fix ‘NameError: name is not defined’ in Python — Beginner Guide 2026 | by Chifahatim | Medium
March 18, 2026 - Always define before use! Cause #2 — Typo in variable name (case-sensitive!) Wrong: age = 25 print(Age) # NameError — 'Age' != 'age'p · Fix: match exactly (Python is case-sensitive). Cause #3 — Using variable outside its scope Wrong: def say_hello(): greeting = "Hello!" say_hello() print(greeting) # NameError — greeting only exists inside function
🌐
TechGeekBuzz
techgeekbuzz.com › home › blog › programming language › python nameerror: name is not defined solution
Python NameError: name is not defined Solution
The solution to the above example is straightforward. We only need to ensure that the variable we are accessing has the same name as we defined earlier in the program. ... Hello World! The following are the most common causes due to which you receive the NameError: name not defined: ... We, humans, have the ability to interpret words with spelling mistakes, but Python does not.
🌐
GitHub
github.com › Datalux › Osintgram › issues › 177
NameError: name 'x' is not defined · Issue #177 · Datalux/Osintgram
February 1, 2022 - NameError: name 'x' is not defined#177 · Copy link · xyzArchie · opened · on Apr 17, 2021 · Issue body actions · This is what I get when I try to run main.py · Reactions are currently unavailable · No one assigned · No labels · No labels · No projects ·
Author: Datalux
🌐
Stack Overflow
stackoverflow.com › questions › 72818275 › error-nameerror-name-x-is-not-defined
python - Error: NameError: name 'x' is not defined - Stack Overflow
My code is: #remove spaces songs['track_name'].apply(lambda x:i.replace(" ","") for i in x) What would the problem be? Error: NameError Traceback...