How to Convert numbers to Words in Python

匿名 (未验证) 提交于 2019-12-03 01:11:01

问题:

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  20) or (Number 

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....

Please help, thanks in advance.

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

回答1:

Your first statement logic is incorrect. Unless Number is 1 or smaller, that statement is always True; 200 is greater than 1 as well.

Use and instead, and include 1 in the acceptable values:

if (Number >= 1) and (Number 

You could use chaining as well:

if 1 

For numbers of 20 or larger, use divmod() to get both the number of tens and the remainder:

tens, below_ten = divmod(Number, 10)

Demo:

>>> divmod(42, 10) (4, 2)

then use those values to build your number from the parts:

return num2words2[tens - 2] + '-' + num2words1[below_ten]

All put together:

def number(Number):     if 1 


回答2:

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


回答3:

Use python library called num2words Link -> HERE



回答4:

I've been also converting numbers to words for some fuzzy matching routines. I used a library called inflect I forked off pwdyson which worked awesome:

https://github.com/pwdyson/inflect.py



回答5:

import math  number = int(input("Enter number to print: "))  number_list = ["zero","one","two","three","four","five","six","seven","eight","nine"] teen_list = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"] decades_list =["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]   if number = 10 and number  19 and number 


回答6:

if Number > 19 and Number  0:     word = num2words1[Number] if Number > 99:     error


回答7:

recursively:

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'} num2words2 = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'] def spell(num):     if num == 0:         return ""     if num 


回答8:

This Did the job for me(Python 2.x)

nums = {1:"One", 2:"Two", 3:"Three" ,4:"Four", 5:"Five", 6:"Six", 7:"Seven", 8:"Eight",\         9:"Nine", 0:"Zero", 10:"Ten", 11:"Eleven", 12:"Tweleve" , 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"} num = input("Enter a number: ") # To convert three digit number into words  if 100 


回答9:

    def giveText(num):     pairs={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:'fourty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety',0:''} # this and above 2 lines are actually single line     return pairs[num]  def toText(num,unit):     n=int(num)# this line can be removed     ans=""     if n 0:         return " "+ans+" "+unit     else:         return " "  num="99,99,99,999"# use raw_input() num=num.replace(",","")# to remove ',' try:     num=str(int(num)) # to check valid number except:     print "Invalid"     exit()  while len(num)


回答10:

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:       return num2words[n]     except KeyError:       try:         return num2words[n-n%10] + num2words[n%10].lower()       except KeyError:         try:           if(n>=100 and n0):               w=w+'And'+n2w(n)             return w               elif(n>=1000):             w=''             w=w+n2w(int(n/1000))+'Thousand'             n=n-int((n/1000))*1000             if(n>0 and n=100):               w=w+n2w(int(n/100))+'Hundred'               n=n-(int(n/100)*100)               if(n>0):                 w=w+'And'+n2w(n)             return w         except KeyError:             return 'Ayyao'   for i in range(0,99999):     print(n2w(i))


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!