NameError: name ‘X’ is not defined in Python

In Python, different types of errors occur when we use inappropriate values, inappropriate data types, or irrelevant data. One of the errors occurs in Python when the variable or function is used without defining it in the program, and this type of error is known as “NameError”.

This write-up will show you different reasons and solutions for “NameError: name X is not defined” in Python with multiple examples. The following contents are explained in this Python guide one by one in detail:

So, let’s get started!

Reason 1: Variable or Function Does Not Exist

One of the main reasons for the “NameError: name ‘X’ is not defined” error in Python is accessing a variable/function that is not present in our script. In Python, to use a variable/function, we need to assign the value or declare it before accessing it in the program. Let’s have a look at the below example to have an idea of this error:

The above code defines a function named “name” using the keyword “def”. This function is accessed and returns the value “5” successfully. But when we tried to access a sample() function, we encountered the “NameError”, which shows that the “name ‘sample’ is not defined”. This is because this function does not exist in the entire program.

Solution: Access the Declared Function or Function Which Exists

To solve this error, the function must be defined before accessing the function.

Code:

Value = 5

def name():
   
    print(Value)
   
def sample():
   
    print(Value)
   
name()

sample()

In the above code, the integer is initialized, and after that, the two different functions are declared using the “def” keyword. Both functions are accessed at the end of the program.

Output:

Because both functions are defined before they are accessed in the code, the stated error has been resolved.

Reason 2: Accessing Before Declaring Function

In the below snippet, the function is accessed before declaring it in the program. The Python interpreter shows an error while executing it because the function or variable can not be accessed before the declaration.

In the above code, the function named “sample” is accessed before being declared.

Solution: Accessed Function After Declaring

Access the function after defining it in the program to fix the stated error.

Code:

Value = 5

def sample():
   
    print(Value)

sample()

In the above code, the function is accessed after the declaration of the function.

Output:

The output shows that the desired function has been accessed successfully.

Reason 3: Misspelled Variable or Function

The “NameError” also arises in the program when the variable or function name is misspelled while accessing or performing an operation on it.

During access to the “sample” function, the function name is misspelled. So, it throws an error.

Note: Python is case-sensitive, so it throws an error if the letter case is ignored when accessing functions.

Solution: Correct the Misspelled Variable or function

In the below code snippet, the error is fixed by correcting the misspelled function name.

Code:

Value = 5

def sample():
   
    print(Value)

sample()

In the above code, the function is accessed at the end of the program using the name of the function with parentheses.

Output:

From the above output, it is verified that the function is accessed successfully without any error.

Reason 4: Not Enclosing String in Quotes

The error arises when the string value is not defined inside the single or double quotation marks.

An example of this is shown below:

In the above code, the string name “David” is not enclosed with single or double quotes. That’s why the “NameError” appeared on the output console of the interpreter.

Solution: Enclosed the String in Single or Double Quotes

To rectify this error, the string value must be placed inside the single or double quotes, as shown in the below code:

Code:

def Students(name):
   
    print('Hello' + name)

Students(' David')

In the above code, a function named “Students” is defined and accepts the parameter value of the name inside the parentheses.

Output:

The string value has been successfully added to another string in the above output.

Reason 5: Not Enclosing Dictionary Key in Quotes

Like simple string values, dictionary key values must be enclosed with single or double quotation marks.

The above code initializes the dictionary value in a variable named “dict_value”. The key “Age” of the dictionary is not enclosed within quotes.

Solution: Enclosed Dictionary Keys Within the Quotes

To fix the “NameError”, wrap the dictionary key within single quotes. Let’s experience it via the following code:

Code:

dict_value = {'Name':'Alex', 'Age': 42}

print(dict_value)

In the above code, the dictionary value is initialized, and the key values are enclosed within the quotes.

Output:

The dictionary value has been successfully created in the above output.

Reason 6: Using Built-in Module With Importing

The error also arises when in the Python program, the inbuilt function of the standard module is used without importing it at the program’s start.

In the above code, the “math.floor()” function of the standard module named “math” is used without importing the math module.

Solution: Import the Module at the Start

To correct this error, the math module is imported at the start of the program, as shown in the below snippet:

Code:

dict_value = {'Name':'Alex', 'Age': 42}

print(dict_value)

In the above code, the math module function “math.floor()” takes the input variable as an argument and returns the floor value.

Output:

The above output shows the floor value of the input number.

This Python Guide has come to an end!

Conclusion

The “NameError: name ‘X’ is not defined” occurs in Python when the operated variable or function is accessed before defining it or not defined in the program. To solve this error, the variable or function must be defined in the program before accessing. The functions and variables must be accessed with the correct spelling. The standard module must be imported at the start or before using/accessing the function. This Python post presented a detailed overview of different reasons and solutions for “NameError: name ‘X’ is not defined” in Python.