Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42169
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Help printing the integers of a longer number |
| Date | 2013-03-28 19:03 +0200 |
| Organization | University of Helsinki |
| Message-ID | <qoty5d7h2x6.fsf@ruuvi.it.helsinki.fi> (permalink) |
| References | <7764e61c-e4cc-413a-a76d-2d37f39abc61@googlegroups.com> |
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) How do you print the digits of left? With print_digits. Why does it work? Because you only call print_digits again when left is closer to zero than num. It's called recursion.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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