You've gotten several good explanations, mainly saying that 0 -> False and not 0 -> True, which is why the while loop exits. You've also gotten advice about how to make your method more robust (i.e. force integer division).
However, as surprising as this may be I'm actually with RR on this one (for a little) -- for code readability's sake, you should make your conditional more readable (i.e. don't depend on the fact that the iterations will take your test value down to 0 which conveniently in this case evaluates to False). This could encourage you in later cases to think that if this result eventually converged to a different number, say the multiplicative identity instead, that the same approach will work (when instead it'll dump you into an infinite loop). You've also gotten the suggestion of typecasting to a string and then looking at the number of characters in the string. This works fine for integers and positive numbers, but not so well for negatives and floats, since both the decimal and negative sign will be counted. You could typecast to a string then strip out '-' and '.' and then count the characters. i.e. def num_digits(n): return len(str(n).replace('-','').replace('.','')) Or typecast to an int if you want to neglect decimals before converting to a string, etc. Or use recursion! >>> def num_digits(n): ... if n == 0: ... return 0 ... else: ... return num_digits(n//10) + 1 ... >>> num_digits(1) 1 >>> num_digits(0) 0 >>> num_digits(10) 2 >>> num_digits(1131341) 7 Why the HELL are you retuning integers in an obviously boolean > function? Are you trying to win Pearl hacker of the year award? Please > folks, always remember... NEVER emulate booleans, always use True/ > False for boolean behaviors. > > def is_foo(): > if something: > return True > return False > NEVER? >>> True = 0 >>> True == False True >>> bool(True) False (I know this is fixed in py3) I think 0 -> False, 1-> True is perfectly reasonable and is fairly common in many languages (only Fortran in the languages I use doesn't use this construct). Not a serious issue in any case. --Jason > *school-bell* > -- > http://mail.python.org/mailman/listinfo/python-list > -- Jason M. Swails Quantum Theory Project, University of Florida Ph.D. Graduate Student 352-392-4032
-- http://mail.python.org/mailman/listinfo/python-list