<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.32.1">
</HEAD>
<BODY>
On Tue, 2011-02-08 at 21:52 -0800, Nanderson wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
def num_digits(n):
    count = 0
    while n:
        count = count + 1
        n = n / 10
    return count

This is a function that basically says how many digits are in a
number. For example,
>>>print num_digits(44)
2
>>>print num_digits(7654)
4

This function counts the number of decimal digits in a positive
integer expressed in decimal format. I get this function ALMOST
completely. The only thing I don't understand is why it eventually
exits the loop, and goes off to the second branch. "while n" is
confusing me. What I am thinking is that if someone puts "while n" the
loop would be infinite. I get what is happening in the function, and I
understand why this would work, but for some reason it's confusing me
as to how it is exiting the loop after a certain number of times. Help
is appreciated, thanks.
</PRE>
</BLOCKQUOTE>
I've become a little annoyed by this thread. Here's a good function that does what you want:<BR>
<TT>def num_digits(n):</TT><BR>
<TT>    """Return number of digits in an iteger."""</TT><BR>
<TT>    count = 1</TT><BR>
<TT>    while n:</TT><BR>
<TT>        n = n // 10</TT><BR>
<TT>        count += 1</TT><BR>
<TT>    return count</TT>
</BODY>
</HTML>