3

Write a function that takes an integer as input argument and returns the integer using words. For example if the input is 4721 then the function should return the string "four seven two one". Note that there should be only one space between the words and they should be all lowercased in the string that you return.

this is my code:

def Numbers_To_Words (number):
    dict = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 0: "zero"}
    output_str = " "
    list = []

#Main Program
number = 4721
result = Numbers_To_Words (number)
print (result)

My question is, How do I separate the numbers and then compare with the dictionary I have created? I know length doesn't work on integer data type. I know the further logic that is to send keys to the dictionary and obtain their respective value. But before that I am stuck here in separating digits of an integer number.

Karan Thakkar
  • 1,007
  • 5
  • 24
  • 41

6 Answers6

9

Use modules for it: https://pypi.python.org/pypi/num2words

Also, check similar questions: How do I tell Python to convert integers into words

You can install the modules and see how it is implemented there.

But your problem is solved like:

def numbers_to_words (number):
    number2word = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six",
            '7': "seven", '8': "eight", '9': "nine", '0': "zero"}
    return " ".join(map(lambda i: number2word[i], str(number)))


print(numbers_to_words(1234))
JRazor
  • 2,707
  • 18
  • 27
6

There is a way that is simpler than the rest:

def number_to_words(number)
    words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
    return " ".join(words[int(i)] for i in str(number))
zondo
  • 19,901
  • 8
  • 44
  • 83
2
def number_to_words(number):
    dict={"1":"one","2":"two","3":"three","4":"four","5":"five","6":"six","7":"seven","8":"eight","9":"nine","0":"zero"}
    s=""
    for c in str(number):
        s+=dict[c]+" "
    #if it's matter that the string won't conatain
    #space at the end then add the next line:
    s=s[:-1]

    return s
pinogun
  • 110
  • 1
  • 7
1

This will help you where you can't import modules for converting numbers to words. This code is for converting numbers from 1 to 1000 both inclusive.

def integer_to_english(number):
    if number>=1 and number<=1000:
        a = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty ','thirty ','fourty ','fifty ','sixty ','seventy ','eighty ','ninty ']
        if number<=20:
            if number%10==0: return a[number]
            else: return a[number]
        elif number<100:
            b=number-20
            r=b%10
            b//=10
            return a[20+b]+a[r]
        elif number<1000:
            if number%100==0:
                b=number//100
                return a[b]+' hundred'
            else:
                r=number%100
                b=number//100
                if r<=20:
                    return a[b]+' hundred'+' and '+a[r]
                else:
                    r=r-20
                    d=r//10
                    r%=10
                    return a[b]+' hundred'+' and '+a[20+d]+a[r]
        elif number==1000:
            return 'one thousand'
        else:
            return -1

number=789
print(integer_to_english(number))
Prudhvi
  • 31
  • 1
  • 7
0

I would use re.sub

>>> def Numbers_To_Words (number):
    dict_ = {'1': "one", '2': "two", '3': "three", '4': "four", '5': "five", '6': "six", '7': "seven", '8': "eight", '9': "nine", '0': "zero"}
    return re.sub(r'\d', lambda m: dict_[m.group()] + ' ', str(number)).strip()

>>> print Numbers_To_Words(4721)
four seven two one
>>> print Numbers_To_Words(98765423)
nine eight seven six five four two three
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
def number_to_words(number):
    names = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    lst = []
    while True:
        lst.insert(0, number % 10) # Prepend
        number /= 10
        if number == 0:    # Do this here rather than in the while condition
           break;          # so that 0 results in ["zero"]

    # If you want a list, then:
    return lst;

    # If you want a string, then:
    return " ".join(lst)       
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52