Nameerror name true is not defined

How do I fix the Python nameerror name true is not defined error message?

Well, this article got your back. Just simply keep on reading.

In this article, we’ll show you how you’re going to resolve the nameerror: name ‘true’ is not defined error.

What is “nameerror: name ‘true’ is not defined”?

The error message nameerror name true is not defined raised because you have a typo or a misspelled variable name “True.” 

For example:

sample_var = true👎
if sample_var == True:
  print("sample_var is true")
else:
  print("sample_var is false")

If you run this code, it will throw a name error that indicates the name true is not defined.

The error message occurs because you are using “True” in a small letter (true). True should be capitalized.

There are instances when “True” is being used in a context though it is not defined. In Python, “True” is a built-in constant that represents the boolean value of true.

Moreover, if you don’t know, Python is case-sensitive, which means that ‘True’ and ‘true’ are not the same. 

If you misspell a variable or function name, Python will not be able to find it, that results to this error.

How to fix “nameerror name true is not defined”?

To fix the nameerror: name ‘true’ is not defined error message in Python, ensure that “True” is capitalized.

Solution 1: Use correct boolean value

You have to check if you are using the correct boolean value. Kindly refer to the illustration below.]

Incorrect code:

sample_var = true

Correct code:

sample_var = True

Here’s the complete code:

sample_var = True
if sample_var == True:
  print("sample_var is true")
else:
  print("sample_var is false")

As you can see, we just capitalize “True” and it resolves the issue. If you are using “true,” you just have to change it to “True.”

Output:

sample_var is true

Solution 2: Use 1 instead

You can also use 1 instead of using True: In Python, the boolean value of true can also be represented by the integer 1.

For example:

sample_var = True
if sample_var == 1:✅
  print("sample is true")
else:
  print("sample is false")

Output:

Sample is true

Solution 3: Define true as a variable

If you want to use the lowercase version of true, you can define it as a variable and assign it the value of True.

For example:



 true = True
if true:
    print("This sample is true")

Output:

This sample is true

Conclusion

The error message nameerror name true is not defined raised because you have a typo or a misspelled variable name “True.” 

This article explores what this error is all about and already provides solutions to help you fix this error.

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

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment