How to resolve TypeError: float() argument must be a string or a number, not ‘NoneType’ in Python

What causes and ways to resolve the TypeError: float() argument must be a string or a number, not ‘NoneType’ in Python. If you are looking for a solution to this problem, this article is for you.

What causes the TypeError: float() argument must be a string or a number, not ‘NoneType’ error?

The error TypeError: float() argument must be a string or a number, not ‘NoneType’ occurs because the parameter passed to the float() function is a None value.

 Example: 

NoneObj = None

# Pass a None value to the float() function
print(float(NoneObj))

Output:

Traceback (most recent call last):
 File "./prog.py", line 4, in <module>
TypeError: float() argument must be a string or a number, not 'NoneType'

Even if you declare a function that returns no value gives the same error.

Example:

def noValueFunc():
    print('1.2')

noValue = noValueFunc()
print(float(noValue))

Output:

Traceback (most recent call last):
 File "./prog.py", line 5, in <module>
TypeError: float() argument must be a string or a number, not 'NoneType'

How to solve this error?

The float() function parameter is a string or a number.

There’s no doubt about this cause the program has informed you that the float() function parameter you passed into the function is a false parameter. Pass float() a string or a number to fix the error.

Example:

intVar = 2
print(float(intVar))

firstStr = '12'
print(float(firstStr))

Output:

2.0
12.0

It can be used with NaN (Not a Number), Infinity, or inf (lowercase and uppercase).

Example:

print(float('inf'))
print(float('NaN'))

Output:

inf
nan

Note: In addition to NaN, inf in the form of a string passed to the float() function is accurate. All other string cases will have an error.

Example:

firstStr = 'learnshareit'
print(float(firstStr))

Output:

Traceback (most recent call last):
 File "./prog.py", line 2, in <module>
ValueError: could not convert string to float: 'learnshareit'

For functions, you need to instantiate a function whose return value must be explicit.

Example:

def ValueFunc():
  return 1

myValue = ValueFunc()
print(float(myValue))

Output:

1.0

The function returns a value of 1, so there is no error when passing the function to the float() function.

Check None value.

To avoid this error, you should check for None before calling float(). There are many ways to check the value None, such as using the isinstance() function, type() function, or relational operators.

Example:

noneObj = None

if isinstance(noneObj, type(None)) is True:
    print('Change the parameter passed to the float() function.')

else:
    result = float(noneObj)
    print(result)

Output:

Change the parameter passed to the float() function.

You can also use the type function to check for the value None.

Example:

noneObj = None

if type(noneObj) is type(None):
    print('Change the parameter passed to the float() function.')

else:
    result = float(noneObj)
    print(result)

Output:

Change the parameter passed to the float() function.

Summary

That is the cause and handling of the error TypeError: float() argument must be a string or a number, not ‘NoneType’ in Python, which I want to convey to you. For None values, ​​it’s hard to control, in my opinion, so be sure you check on it before doing anything else. Hope you get it fixed soon.

Leave a Reply

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