Number to English Words
Write a program to convert a non-negative integer
The input will be a single line containing an integer
The output should be a single line containing the representation of the English words of number
For example, if the given number is 123, your code should print the English words representation, which is
One Hundred Twenty Three
Sample Input 1
123
Sample Output 1
One Hundred Twenty Three
Sample Input 2
10005
Sample Output 2
Ten Thousand Five
Sample Input 3
1234567891
Sample Output 3
One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One
for this question you have answered like:
but we are getting "ModuleNotFoundError: No module named 'num2words'"
import num2words
print(num2words.num2words(input()))
low = ["Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine"]
then = ["Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"]
mid = ["Ten",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"]
high = ["Hundred",
"Thousand",
"Million",
"Billion"]
def hund2word(hund: int):
word = ""
a = hund % 100
if 0 != a and a < 10:
word += low[a]
elif 10 <= a < 20:
word += then[a % 10]
elif 20 <= a:
word += mid[a // 10 - 1]
if a % 10 != 0:
word += ' ' + low[a % 10]
b = hund // 100
if b != 0:
word = low[b] + ' ' + high[0] + ' ' + word
return word
def num2word(num: int):
if num == 0:
return low[num]
else:
store = list()
while num != 0:
store.append(num % 1000)
num //= 1000
word = ""
i = len(store)
for num in reversed(store):
word += hund2word(num)
if i != 1:
word += ' ' + high[i-1] + ' '
i -= 1
return word
print(num2word(123))
print(num2word(10005))
print(num2word(1234567891))
Comments
Leave a comment