TypeError: object of type ‘float’ has no len()

The python error TypeError: object of type ‘float’ has no len() occurs while attempting to find the length of an object that returns ‘float’. The python length function can not be used in the ‘float’ type objects. The Length function is used for objects that store multiple objects. Hence, the length function should be used in objects which can store multiple float values

The length function is used in a structured object such as list, tuple, and dictionary to find the length of the string or the number of objects.The length function does not accept data types such as int, float, long, boolean, and complex data types. If the length function is invoked on unsupported objects then the python interpreter will throw the error TypeError: object of type ‘float’ has no len() .



Exception

The error TypeError: object of type ‘float’ has no len() will show the stack trace as below. The stack trace will show the line at which the error occurred.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print len(s)
TypeError: object of type 'float' has no len()
[Finished in 0.0s with exit code 1]


How to reproduce this error

Create a python variable assigned with a float value. If the length function is invoked for the float variable, the error TypeError: object of type ‘float’ has no len() will be thrown.

s=5.0
print len(s)

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print len(s)
TypeError: object of type 'float' has no len()
[Finished in 0.0s with exit code 1]


Solution 1

The python variable assigned to the float value should not be used in the length function. The python variable should be converted to objects such as list, tuple, set, dictionary, etc. In those objects, the float value should be added

s= [5.0]
print len(s)

Output

1
[Finished in 0.0s]


Solution 2

If the intended data type of the variable is known as float, the data type should be checked before the length function is invoked. If the data type is float, take an alternate flow. Otherwise, the length method is called with the python variable.

s= 5.0
print type(s)
if isinstance(s, float) : 
	print "Value is float"
else:
	print (len(s))

Output

<type 'float'>
Value is float
[Finished in 0.0s]


Solution 3

If the intended data type of the variable is unknown, the expected data type should be checked before the length function is invoked. If the data type is a list of objects, the length method is called with the python variable. Otherwise, take an alternate flow.

s= 5.0
print type(s)
if type(s) in (list,tuple,dict, str): 
	print (len(s))
else:
	print "not a list"

Output

<type 'float'>
not a list
[Finished in 0.0s]


Solution 4

The try and except block is used to capture the unusual run time errors. If the python variable contains expected value, then it will execute in the try block. If the python variable contains the unexpected value, then the except block will handle the error.

s= 5.0
print type(s)
try : 
	print (len(s))
except :
	print "not a list"

Output

<type 'float'>
not a list
[Finished in 0.1s]



Related Articles

Leave a Reply

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