Nameerror name is not defined

The “nameerror name is not defined” is a common type of error raised when working in Python.

If this is your first time encountering this error, and wondering how to resolve it?

Continue reading because, in this article, we’ll walk you through how you will fix the “nameerror name is not defined.”

But before we dive into the solutions, let’s first understand what this error means and why it occurs.

What is NameError?

A NameError occurs if you try to use a variable or a function name that is not valid.

A name can be either related to a built-in function or to something you define in your program (e.g. a variable or a function).

What is “nameerror name is not defined”?

The nameerror: name is not defined occurs when you try to use a variable or a function name that has not been defined yet in your code.

Therefore, the interpreter does not recognize the name of the variable or function because it has not been assigned a value or defined with a function definition statement.

For example, if you use a variable in your code without declaring it first. Automatically, you will get this error message.

Why does this error occur?

“Nameerror name is not defined” can occur in your code because of some factors, such as:

❌ Misspelling the name of a variable or function.

❌ Defining the variable or function after it has been used.

❌ Forgetting to define the variable.

❌ When the variable or function is defined in a different scope from where it is being used.


Using reserved keywords: using reserved keywords, such as if, else, or for as variable names can cause this error.

How to fix “nameerror name is not defined”?

In this section, you’ll get the solutions that you can use to resolve the error:

Solution 1: Check the spelling of the variable or function name

For example:

website = ["Itsourcecode"]

print(websites)

Output:

NameError: name 'websites' is not defined

In order to resolve this error, you just have to fix the typo.

Ensure that the spelling of the variable or function name is correct.

Corrected code:

website = ["Itsourcecode"]

print(website)

Output:

['Itsourcecode']

Solution 2: Define the variable or function before using it

For example:

a = 100
 = a + b
print(b)
c = 50

Output:

NameError: name 'b' is not defined

To fix this error, you have to define the variable or function before using it in the code.

Corrected code:

a = 100
b = 50
c = a + b
print(c)

Output:

150

Solution 3: Define the variable

When you forget to define the variable, certainly you will face this error.

For example:

for w in website:
print(w)

Output:

NameError: name 'website' is not defined

To fix this error, you have to define “website” before using it in your code.

Corrected code:

website = ["Itsourcecode"]

for w in website:
 print(w)

Output:

Itsourcecode

Solution 4: Check the scope of the variable or function

NameError can also raise when the variable or function is defined in a different scope than where it is being used.

def func():
    a = 50

func()
print(b)

Output:

NameError: name 'b' is not defined

To fix this error, you have to fix this error, ensure that the variable or function is defined in the correct scope.

Corrected code:

def func():
    a = 50
    print(a)
func()

Output:

50

Solution 5: Print a single word with single or double quotes

When you are trying to print a single word in Python, you have to print it with either single or double quotes.

For example:

print(website)

As you can see, the code tries to print the word “website” to the console. However, it results to:

Output:

NameError: name 'website' is not defined

To fix this error, you have to enclose the word website with double quotes.

Corrected code:

print("website")

Output:

website

Solution 6: Use global keyword

When you are trying to access a variable that is defined in a different scope.

Alternatively, you can use the global keyword to indicate that the variable is defined globally.

For example:

a = 10

def func():
    a += 1

func()

Corrected code:

Ensure to declare it as global using the global keyword.

a = 10

def func():
    global a
    a += 1

func()

print(a)

Output:

11

Conclusion

The nameerror: name is not defined occurs when you try to use a variable or a function name that has not been defined yet in your code.

This article already provided several effective solutions that will help you to fix the “nameerror name is not defined.”

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊