您的位置:首页 > 编程语言 > Python开发

利用python写出的算法 关于输入非正数的整数打印出其英语

2018-01-18 12:05 429 查看
这里楼主只做出了小于15位数的英语打印:

#利用输入的非负数的数字打印出单词(小于15位的数字)
#划分成三位数一份进行读取
s=input()
dict1={1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine"}#没三位的第一位和第三位数的的用法
dict2={10:"ten",11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",16:"sixteen",17:"seventeen",18:"eighteen",19:"nineteen"}#当后两位为十几的读法
dict3={1:"ten",2:"twenty"
4000
,3:"thirty",4:"forty",5:"fifty",6:"sixty",7:"seventy",8:"eighty",9:"ninety"}#弟二位数大于1的读法
dict4={3:"hundred",4:"thousand",7:"million",10:"billion",13:"trillion"}#所有数的长度一定时读法
l=len(s)#用于作减
alllenth=l#统计总共的长度   用于取值
while l>0:
if (alllenth==1 and s[0]==0) or int(s[0:])==0:#当输入的数字都是0和只有一位数时的0  这里考虑到0的特殊
print("zero")
l-=l-1
if int(s[alllenth-l:])>0:#这是一个大于0的数字
if  l%3==1 or (int(s[alllenth-l])!=0 and l%3==0 and l/3==1): #在第1位上的数字或者 只有1组三维的数字 比如301 但第三个数字不为0
print(dict1.get(int(s[alllenth-l]),""),end=" ")
if int(s[alllenth-l])==0 and l%3==0 and l/3==1:#只有三位数   第一位为0   只有一组三维数的情况
l-=1
if l%3==0 and l/3!=1 and int(s[alllenth-l])!=0:#不止一组三位数  而且第一个位置上的数不为0
print(dict1.get(int(s[alllenth-l]),""),end=" hundred  ")
if l%3==2:#第二个位置上的数字的情况
if int(s[alllenth-l])>1 and int(s[alllenth-l+1])>0:#第二个位置上的数字大于1 而且第三个位置上的数字大于0的情况
print(dict3.get(int(s[alllenth - l]),""),end=" ")
if int(s[alllenth-l+1])==0:#第三个位置上的数为0的情况
print(dict3.get(int(s[alllenth - l]),""),end=" ")
l-=1
if int(s[alllenth-l])==1:#第二个位置上的数为1的 情况
print(dict2.get(int(s[alllenth - l:alllenth - l + 2]),""),end=" ")
l-=1
if l%3==1 and l<=alllenth-2 and int(s[alllenth - l - 2:alllenth - l + 1])==0:#排除这一组的三个数都为0的情况
print("",end="")
else:
print(dict4.get(l,""),end=" ")
l-=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐