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


Groups > comp.lang.python > #42173

Re: Help printing the integers of a longer number

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'example:': 0.03; '"if': 0.09; 'function,': 0.09; 'subject:number': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'be:': 0.16; 'behaviour.': 0.16; 'division.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'infinitely': 0.16; 'left,': 0.16; 'numbers;': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'integer': 0.24; 'sorry,': 0.24; 'fine': 0.24; '(or': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'division': 0.31; 'prints': 0.31; 'writes:': 0.31; 'fri,': 0.33; 'subject:the': 0.34; 'received:209.85': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'should': 0.36; 'positive': 0.37; 'received:209': 0.37; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'eventually': 0.60; 'negative': 0.60; 'removing': 0.60; 'numbers': 0.61; 'email addr:gmail.com': 0.63; 'due': 0.66; 'mar': 0.68; 'special': 0.74; 'subject:printing': 0.84; 'continue.': 0.91; 'thing,': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=f7pmrw2HzIjDhQzYsHQOJYoq5/GyMmr9GdYgrRHJqvI=; b=iq+yWeNyv0lVEWiazyQel/vC91X2bYVs90BFJES+zcaL7Oipnsr1Ade4Gvx0a/CjHJ agiB0yrYpevqj9Ha0GLp5u7LA/QtKUiSLweYCNkzqs147GghZno2k3p7rEJzHuLcHeCp ksRb4T6xQDY86F/EB1Nmmo+I8K0kcwi/GDKNGkMKPh4d7kg9IyiEO4ei5K/9ELkvCiyA 30FC9g8mOzWO5v7w5wp250ERn62345rKCRrS11wDCaLnJWEBW5kQUIN531kfhtu3E7tc UbpNkYoOUw3U67WTv6GCoKEXnVSuzWFiOKKdbJJNyJL2VidilzlGD4l6kWoUg7yaaQi5 jq9A==
MIME-Version 1.0
X-Received by 10.52.16.211 with SMTP id i19mr6985487vdd.91.1364491179960; Thu, 28 Mar 2013 10:19:39 -0700 (PDT)
In-Reply-To <qottxnvh2l4.fsf@ruuvi.it.helsinki.fi>
References <7764e61c-e4cc-413a-a76d-2d37f39abc61@googlegroups.com> <qoty5d7h2x6.fsf@ruuvi.it.helsinki.fi> <qottxnvh2l4.fsf@ruuvi.it.helsinki.fi>
Date Fri, 29 Mar 2013 04:19:39 +1100
Subject Re: Help printing the integers of a longer number
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.3904.1364491182.2939.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364491182 news.xs4all.nl 6844 [2001:888:2000:d::a6]:60485
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42173

Show key headers only | View raw


On Fri, Mar 29, 2013 at 4:11 AM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
> Jussi Piitulainen writes:
>
>> khaosyt@gmail.com writes:
>>
>> > I want to print the individual numbers of a large number using
>> > division and modulus division.
>> >
>> > For example:
>> >
>> > Enter a positive integer: 54321
>> > 5
>> > 4
>> > 3
>> > 2
>> > 1
>>
>> Those numbers are called the digits of the large number.
>>
>> With divmod(54321, 10) you get both the number that is "left" after
>> removing the last digit, and the last digit:
>>
>> >>> left, last = divmod(54321, 10)
>> >>> left
>> 5432
>> >>> last
>> 1
>>
>> Define a function, print_digits(num), that prints the digits of the
>> non-negative integer num. Zero turns out fine so let's allow zero:
>>
>> def print_digits(num):
>>    left, last = divmod(num, 10)
>>    if left < 0: print the digits of left
>>    print(last)
>
> Blush. That should be:
>
>     ...
>     if left > 0: ...
>     ...
>
> (Or just "if left" because left will eventually be 0, positive numbers
> are true values, and 0 is a false value.)
>
> Sorry about that.

Sorry, I just nitpicked that very thing, hehe :)

Note that this doesn't work with negative numbers; it'll infinitely
recurse, due to divmod's behaviour. You'd need a special trap in there
to handle that:

if num<0:
	print("-")
	num=-num
# and continue.

ChrisA

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


Thread

Help printing the integers of a longer number khaosyt@gmail.com - 2013-03-28 07:39 -0700
  Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 01:48 +1100
  Re: Help printing the integers of a longer number Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-28 10:48 -0400
  Re: Help printing the integers of a longer number Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-28 19:03 +0200
    Re: Help printing the integers of a longer number Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-28 19:11 +0200
      Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 04:19 +1100
    Re: Help printing the integers of a longer number Chris Angelico <rosuav@gmail.com> - 2013-03-29 04:17 +1100

csiph-web