P2.4.3\(\pi\) read aloud
Using a tuple of strings naming the digits 0 – 9, create a Python program which outputs the representation of \(\pi\) as read aloud to 8 decimal places:
three point one four one five nine two six five
Show solution
This code illustrates one approach:
import math
num = ('nought', 'one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine')
print('three point ', end='')
for i in str(math.pi)[2:10]:
print(num[int(i)], end=' ')
print()
This program turns math.pi into a string and picks off the first eight decimal places to use as indexes to the tuple of number names. The output is:
three point one four one five nine two six five