On Fri, Mar 29, 2013 at 4:11 AM, Jussi Piitulainen <jpiit...@ling.helsinki.fi> wrote: > Jussi Piitulainen writes: > >> khao...@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 -- http://mail.python.org/mailman/listinfo/python-list