Answer to Question #198919 in Python for Narendra

Question #198919

Number to English Words

Write a program to convert a non-negative integer


N to its English words representation.Input

The input will be a single line containing an integer


N. Note: The input number will not be greater than 1010.Output

The output should be a single line containing the representation of the English words of number


N. See the sample input and output for the English words of numbers in places.Explanation

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()))





1
Expert's answer
2021-05-26T23:29:12-0400
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))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS