<br><br><div class="gmail_quote">On Wed, Feb 9, 2011 at 5:34 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On 09/02/2011 21:42, Jason Swails wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
You've gotten several good explanations, mainly saying that 0 -> False<br>
and not 0 -> True, which is why the while loop exits.  You've also<br>
gotten advice about how to make your method more robust (i.e. force<br>
integer division).<br>
<br>
However, as surprising as this may be I'm actually with RR on this one<br>
(for a little) -- for code readability's sake, you should make your<br>
conditional more readable (i.e. don't depend on the fact that the<br>
iterations will take your test value down to 0 which conveniently in<br>
this case evaluates to False).  This could encourage you in later cases<br>
to think that if this result eventually converged to a different number,<br>
say the multiplicative identity instead, that the same approach will<br>
work (when instead it'll dump you into an infinite loop).<br>
<br>
You've also gotten the suggestion of typecasting to a string and then<br>
looking at the number of characters in the string.  This works fine for<br>
integers and positive numbers, but not so well for negatives and floats,<br>
since both the decimal and negative sign will be counted.  You could<br>
typecast to a string then strip out '-' and '.' and then count the<br>
characters.  i.e.<br>
<br>
def num_digits(n):<br>
    return len(str(n).replace('-','').replace('.',''))<br>
<br>
</blockquote></div>
Or:<br>
<br>
def num_digits(n):<br>
    return len(str(abs(n)).replace('.','')) </blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im"><br>

<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Or typecast to an int if you want to neglect decimals before converting<br>
to a string, etc.<br>
<br>
</blockquote></div>
[snip]<br>
Python doesn't have typecasting. :-)<br></blockquote><div><br>Because these basic types are not mutable?  <excuse> Most of my work has to be in Fortran, so I'm a relative newcomer to Python.  When I don't need Fortran-y performance it's much nicer (obviously to anyone that's used them both)!  Still don't know much deeper than Python's cosmetic surface at this point. </excuse><br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><font color="#888888">
-- <br></font><div><div></div><div class="h5">
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Jason M. Swails<br>Quantum Theory Project,<br>University of Florida<br>Ph.D. Graduate Student<br>352-392-4032<br>