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