Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55726

Re: Easy function, please help.

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mhrivnak@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'int': 0.05; 'wed,': 0.05; 'exits': 0.07; 'function,': 0.07; 'python': 0.08; 'throw': 0.09; 'this:': 0.10; '>>>': 0.11; 'am,': 0.13; 'loop': 0.13; 'wrote:': 0.14; 'def': 0.14; '"while': 0.16; 'division,': 0.16; 'evaluates': 0.16; 'int):': 0.16; 'int.': 0.16; 'integer.': 0.16; 'stops.': 0.16; 'subject:function': 0.16; 'type:': 0.16; '\xa0while': 0.16; 'feb': 0.16; 'example:': 0.19; 'sender:addr:gmail.com': 0.22; 'integer': 0.23; 'loop,': 0.23; 'header:In-Reply-To:1': 0.23; 'michael': 0.24; 'subject:please': 0.24; "didn't": 0.25; 'looked': 0.25; '(in': 0.25; 'work,': 0.26; 'function': 0.27; 'second': 0.27; 'happening': 0.28; 'least': 0.29; 'example': 0.29; 'message- id:@mail.gmail.com': 0.29; 'times.': 0.29; 'appreciated,': 0.30; 'decimal': 0.30; 'digits': 0.30; 'exiting': 0.30; 'planned': 0.30; 'to:addr:python-list': 0.31; 'url:mailman': 0.31; 'says': 0.31; 'expression': 0.31; 'thinking': 0.32; 'thanks.': 0.32; 'probably': 0.32; 'raise': 0.34; 'someone': 0.34; 'pass': 0.34; 'format.': 0.35; 'using': 0.35; 'example,': 0.35; 'but': 0.36; 'help': 0.38; 'works': 0.38; 'received:209.85': 0.38; 'either': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'subject:: ': 0.39; 'some': 0.39; 'me.': 0.39; 'url:python': 0.39; 'to:addr:python.org': 0.40; "it's": 0.40; 'your': 0.61; 'cast': 0.64; 'act': 0.66; '12:52': 0.84; 'subject:Easy': 0.84; 'well:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; bh=mID8f5rK9b6GkURs035KmsIfMPQgPevlLkzmUtZDNQM=; b=u8PCI+/uzBuFsj/XDvh8Qt7JSpcoNQDJNbL2mv/hVktW7FVucBA86dj0178W6Ae58P sJVraDaTg6C3BUi+EoLCmAT9hUQPaAX6lpQTJX5qnAhCYspYV1L7YXzvLxWv+F12fT9w +JR5aEkvpdNS6iEzLvgnDy62nsugvDcsMjPdE=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type :content-transfer-encoding; b=K5q8W6iPm7NGoA9NZVcpFD1DsEO96myyP9L8kPpwjktljL5LA7zz10m60onaih+Yx0 i6Kd1jmD3ce5cS7ipiUxB0QhFU+SrYhh0Yn6XAcycj7tKn0nhJ529KX/1qEveF4YZzJL +ihk9p3JbvzX4vNZXe0EqDX+zUG8y4h4ClkFw=
MIME-Version 1.0
Sender mhrivnak@gmail.com
In-Reply-To <8e0efff7-4d38-4a85-9653-2178388603d4@y36g2000pra.googlegroups.com>
References <8e0efff7-4d38-4a85-9653-2178388603d4@y36g2000pra.googlegroups.com>
Date Wed, 9 Feb 2011 01:29:33 -0500
X-Google-Sender-Auth WjzgZzws4L4U8zg-nVb8zgOMg18
Subject Re: Easy function, please help.
From Michael Hrivnak <mhrivnak@hrivnak.org>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.29.1297232975.1633.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 82.94.164.166
X-Trace 1297232975 news.xs4all.nl 41117 [::ffff:82.94.164.166]:38095
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55726

Show key headers only | View raw


Your function only works if n is an integer.  Example:

>>> num_digits(234)
3
>>> num_digits(23.4)
325

When doing integer division, python will throw away the remainder and
return an int.  Using your example of n==44, 44/10 == 4 and 4/10 == 0

Before each iteration of the while loop, the given expression (in this
case just n) is evaluated as a boolean.  Your function would act the
same if it looked like this:

def num_digits(n):
  count = 0
  while bool(n):
      count = count + 1
      n = n / 10
  return count

0 of course evaluates to False as a boolean, which is why the while loop stops.

Just for kicks, this function would work about as well:

def num_digits(n):
   return len(str(n))

And if either of these were a real function you planned to use, you'd
probably want to either cast n as an int ( int(n) ) or at least check
its type:

if not isinstance(n, int):
   raise TypeError("WTF you didn't pass me an int")

Michael

On Wed, Feb 9, 2011 at 12:52 AM, Nanderson
<mandersonrandersonanderson@gmail.com> wrote:
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Easy function, please help. Michael Hrivnak <mhrivnak@hrivnak.org> - 2011-02-09 01:29 -0500
  Re: Easy function, please help. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-02-10 07:08 +0000
  Re: Easy function, please help. drygal <drygalski@googlemail.com> - 2011-02-09 22:53 -0800

csiph-web