Python Forum

Full Version: Printing digits after the decimal point one by one
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I recently began learning Python and I am trying to write a program in which I print the digits after the decimal point one by one once the user enters a non-integer value.

So far I have this:

for x in str(num).split(".")[1]:
print(int(x))

With this, if num = 0.089 as an example, it would give me:

0

8

9

However, I want it like this:

9

8

0

How should I change my code? Thanks.
You could wrap your split-off string in a reversed(), or you could iterate over it backward.

>>> for x in reversed(str(0.089).split(".")[1]): print(x)
...
9
8
0
>>> for x in str(0.089).split(".")[1][::-1]: print(x)
...
9
8
0