Okay so as a complete newbie... Currently I am at chapter 6 of a book called How to Think like a Computer Scientist (http://openbookproject.net/thinkcs/python/english2e/ch06.html)

and I for some reason can't figure out this exercise:

Make this doctest pass:

def print_digits(n):
    """
      >>> print_digits(13789)
      9 8 7 3 1
      >>> print_digits(39874613)
      3 1 6 4 7 8 9 3
      >>> print_digits(213141)
      1 4 1 3 1 2
    """

And I seriously have no idea. I must be missing something because so far, everything in the book has been rather basic so the answer probably shouldn't be something too long nor advanced. The only thing I've found so far on the internet is this:

(int(str(n)[::-1]

Unfortunately there are two(but mainly one, the second problem is just a minor thing) problems:
1) The doctest contains spaces between the numbers!

2) The n[::-1] thing is something the book have never mentioned or hinted at and I it a bit weird since from what I can remember nothing else so far has forced me to use stuff the book haven't covered. That doesn't matter very much though, I just want to find the solution for this thing because I am sitting in the heat right now and feeling so frustrated I can't concentrate at all.

Recommended Answers

All 2 Replies

How abot divmod or modulo % and integer division // ?
You might find reading my base conversion snippet helpful.

You can also make the str(n)[::-1] work by doing

' '.join(str(n)[::-1])

Thanks! I'll probably just stick with the .join thingy for now though! That base conversion code made my head spin :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.