+2 votes
in Programming Languages by (73.8k points)

I am running the following small python code to check the number of digits in the random number I generated. But I am getting error: "TypeError: object of type 'int' has no len()"

>>> import random

>>> a=random.randrange(1, 1000)

>>> b=len(a)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: object of type 'int' has no len()

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can figure out from the error message that you cannot use len() function on integer. Convert integer to string before using len() function.

>>> a=random.randrange(1, 1000)
>>> b=len(str(a))
>>> a
848
>>> b
3


...