用python简单实现将整数转化为英文字符串

#!/bin/env python
 
import string
while True:
    number = int(raw_input('please input a number:').strip())
    if number > 1000 or number <= 0:
        print 'please input mid 0 to 999 number!!!!!!!!!!!'
        continue
    else :
        break

num = []
num1={'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':'thirten',\
      '14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen',\
      '19':'nineteen',}
num2=(' ',' ','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety')
num3=('hundred','thousand')
if 0 < number <= 19:
    num.append(num1[str(number)])
elif 20 <= number <=99:
    shi = number//10
    gewei = number%10
    num.append(num2[shi])
    num.append('-')
    num.append(num1[str(gewei)])

elif number==100:
    num.append('one thousand')
else :
    gewei = number%10
    number=number//10
    shiwei=number%10
    number=number//10
    baiwei=number
    num.append(num1[str(baiwei)])
    num.append(' ')
    num.append(num3[0])
    num.append(' ')
    num.append(num2[shiwei])
    num.append('-')
    num.append(num1[str(gewei)])

str_ = ''.join(num)
print str_

转载于:https://my.oschina.net/u/2313186/blog/533418

你可能感兴趣的:(用python简单实现将整数转化为英文字符串)