+2 votes
in Programming Languages by (7k 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 (13.5k 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
848
>>> b
3

...