How To Resolve NameError: Name ‘true’ Is Not Defined In Python

NameError: name 'true' is not defined in Python

This article will give you some solutions to fix the NameError: name ‘true’ is not defined in Python. Follow it and solve your problem.

What causes the NameError: name ‘true’ is not defined in Python?

Bool is a simple and common data type in Python. However, this is a critical data type to give you a foundation for learning about control structures (if-elif-else, while, for). Bool type in Python is a data type with only two values ​​’True’ and ‘False’. And ‘True’, and ‘False’ are also two keywords in Python.

‘NameError’ is an error when you use a variable, function, or module that is not declared or you are not using it properly.

The NameError: name ‘true’ is not defined error happens when you misspell the keyword ‘True’ (you didn’t capitalize the first letter ‘true’).

Example:

def divisibleBy2(valNumber):
   if valNumber % 2 == 0:
      return true
      
print(divisibleBy2(4))

Output:

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
  File "./prog.py", line 3, in divisibleBy2
NameError: name 'true' is not defined

How to solve this error?

Capitalize boolean constants

According to the reason given above, the solution now for you is to capitalize the first letter of two bool values. Correct spelling would be: ‘True’ and ‘False’.

Example:

  • Initialize the variable as a natural number.
  • Declare a function that is divisible by 2. Numbers divisible by 2 return ‘True’, and numbers that are not divisible by 2 return ‘False’.
def divisibleBy2(valNumber):
  if valNumber % 2 == 0:
    return True    
  else:
    return False
   
print('Result1:', divisibleBy2(4))
print('Result2:', divisibleBy2(3))

Output:

Result1: True
Result2: False

Use the bool() function

If you don’t want to use the keywords ‘True’ and ‘False’, then you can use bool() – Python’s built-in function. The bool() function also returns a boolean value.

Syntax:

bool(value)

Note: Some values are considered False by default in Python, such as None, empty map, empty sequence, and so on.

Example:

myNumber1 = 4
myNumber2 = 2   
myNumber3 = 3

# Use the bool() function to check 
print('Result1:', bool(myNumber1 % myNumber2 == 0))
print('Result2:', bool(myNumber1 % myNumber3 == 0))

Output:

Result1: True
Result2: False

Use the isinstance() function

You can also use the isinstance() function to resolve errors.

Syntax:

isinstance(object , classinfo)

Parameters:

  • object the number you want to check.
  • classinfo: class, type, or tuple.

The isinstance() method will return True for the specified type and False in all other cases.

Example:

# Use the isinstance() function to check if the 1st parameter is a subclass of the 2nd parameter
print('Result1:', isinstance(4, int))
print('Result2:', isinstance(4.3, float))
print('Result3:', isinstance(4.3, int))

Output:

Result1: True
Result2: True
Result3: False

Summary

Here are the methods to help you solve the NameError: name ‘true’ is not defined in Python. If you have any questions about this issue, please leave a comment below. I will answer your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *