I am trying to write a function that counts the number of digits in a given number. This is my function:
def num_digits(n):
"""
>>> num_digits(12345)
5
>>> num_digits(0)
1
>>> num_digits(-12345)
5
"""
count = 0
while n > 0:
if n == 0:
count += 1
count += 1
n = n/10
return count
if __name__=="__main__":
import doctest
doctest.testmod(verbose=True)
But this function is not working. What should the condition be for the while loop?