Help printing the integers of a longer number

Chris Angelico rosuav at gmail.com
Thu Mar 28 13:17:19 EDT 2013


On Fri, Mar 29, 2013 at 4:03 AM, Jussi Piitulainen
<jpiitula at ling.helsinki.fi> wrote:
> 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.

An elegant solution, but buggy, I'm afraid... fortunately it's a
trivial problem. The comparison should be left>0. :)

ChrisA



More information about the Python-list mailing list