+2 votes
in Programming Languages by (28.1k 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

0 votes
by (48.7k points)

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
>>> b