Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numbers to words without using num2word library?

I need to turn numbers from 1 - 99 into words. This is what I got so far:

num2words1 = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', \             6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', \             11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', \             15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen'} num2words2 = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety']  def number(Number):      if (Number > 1) or (Number < 19):         return (num2words1[Number])     elif (Number > 20) or (Number < 99):         return (num2words2[Number])     else:         print("Number Out Of Range")         main()  def main():     num = eval(input("Please enter a number between 0 and 99: "))     number(num) main() 

Now, the BIGGEST problem that I have so far is that the if, elif and else statements DO NOT seem to work. Only the first if statement runs.

The second problem is creating the string version of the numbers from 20-99....

P.S. Yes, I know about the num2word library, but I am not allowed to use it.

like image 205
user1919840 Avatar asked Oct 21 '13 20:10

user1919840


People also ask

How do you convert digits to words in Python?

num2words module in Python, which converts number (like 34) to words (like thirty-four). Also, this library has support for multiple languages. In this article, we will see how to convert number to words using num2words module.

How do I convert numbers to text in Word?

Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50). Press Enter to confirm the formula.

How do I print numbers in words?

Logic to print number in words:Extract the last digit of a given number by performing modulo division by 10 and store the result in a variable. Now create a switch case to print digit 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Remove the last digit of a number. Repeat steps 3 to 5 until the number becomes 0.


1 Answers

You can make this much simpler by using one dictionary and a try/except clause like this:

num2words = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', \              6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', \             11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', \             15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', \             19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', \             50: 'Fifty', 60: 'Sixty', 70: 'Seventy', 80: 'Eighty', \             90: 'Ninety', 0: 'Zero'}  >>> def n2w(n):         try:             print num2words[n]         except KeyError:             try:                 print num2words[n-n%10] + num2words[n%10].lower()             except KeyError:                 print 'Number out of range'  >>> n2w(0) Zero >>> n2w(13) Thirteen         >>> n2w(91) Ninetyone >>> n2w(21) Twentyone >>> n2w(33) Thirtythree 
like image 73
dansalmo Avatar answered Oct 13 '22 11:10

dansalmo