Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23000
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 42
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-24 11:08
文章分类
文章存档

2015年(3)

2014年(1)

我的朋友

分类: Python/Ruby

2015-02-04 10:39:41

题目:使用列表,编写将0~1000范围的数字翻译为英文的程序,例如输入89,输出eighty-nine。

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. '''numbers to english'''

  3. #for less than 21
  4. list1=['zero','one','two','three','four','five','six','seven','eight','nine',
  5.        'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen',
  6.        'seventeen','eighteen','nineteen','twenty']
  7. #for second bit
  8. list2=['','','twenty','thirty','forty','fifty',
  9.        'sixty','seventy','eighty','ninety','hundred']
  10. #for 1000
  11. list3=['','one thousand']

  12. #filter th input
  13. def num_input():
  14.     while True:
  15.         num=raw_input('Enter a number in 0~1000: ')
  16.         num=num.strip()
  17.         if num=='':
  18.             print 'no value'
  19.             continue
  20.         try:
  21.             num2=int(num)
  22.         except Exception:
  23.             print 'TypeError'
  24.             continue
  25.         if num2<0 or num2>1000:
  26.             print 'over range'
  27.         else:
  28.             break
  29.     return num

  30. #translate number to english
  31. def translate(num):
  32.     n1=int(num)
  33.     if n1<21 and n1>=0: #one word
  34.         r1=list1[n1] #use index
  35.         return r1
  36.     elif n1>20 and n1<100: #use second bit
  37.         n2=repr(n1) #to string
  38.         r2=list2[int(n2[-2])] #the second bit
  39.         r3=list1[int(n2[-1])] #the first bit
  40.         if int(n2[-1])==0: #only second bit and first bit is 0
  41.             return r2
  42.         else:
  43.             return r2+'-'+r3
  44.     elif n1>=100 and n1<1000: #use third bit
  45.         n3=repr(n1)
  46.         r4=list1[int(n3[-3])] #the third bit
  47.         r5=list2[int(n3[-2])] #the second bit
  48.         r6=list1[int(n3[-1])] #the first bit
  49.         if int(n3[-1])==0 and int(n3[-2])!=1: #first is 0
  50.             return r4+' hundred-'+r5
  51.         elif int(n3[-2])==1:
  52.             r7=list1[int(n3[-2:])] #sec+fir less than 20,only use list1
  53.             return r4+' hundred-'+r7
  54.         else:
  55.             return r4+' hundred-'+r5+'-'+r6
  56.     elif n1==1000:
  57.         r8=list3[1] #use fourth
  58.         return r8

  59. if __name__=='__main__': #main()
  60.     print translate(num_input())
测试记录:
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 0
zero
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 1
one
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 13
thirteen
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 311
three hundred-eleven
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 721
seven hundred-twenty-one
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 899
eight hundred-ninety-nine
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: 1000
one thousand
[root@ahqz py]# python num-to-english.py
Enter a number in 0~1000: a
TypeError
Enter a number in 0~1000: b
TypeError
Enter a number in 0~1000:
no value
Enter a number in 0~1000: 666
six hundred-sixty-six
[root@ahqz py]#
阅读(970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~