码迷,mamicode.com
首页 > 其他好文 > 详细

Speech Module

时间:2014-08-05 10:49:49      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   io   for   cti   div   python   log   

Speech Module

 

 1 FIRST_TEN = ["one", "two", "three", "four", "five", "six", "seven",
 2              "eight", "nine"]
 3 SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
 4               "sixteen", "seventeen", "eighteen", "nineteen"]
 5 OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
 6               "eighty", "ninety"]
 7 HUNDRED = "hundred"
 8 
 9 
10 def checkio(number):
11     spoken = []
12     
13     hundred_bit = number / 100
14 
15     if hundred_bit > 0:
16         spoken.append(FIRST_TEN[hundred_bit - 1])
17         spoken.append(HUNDRED)
18 
19     remain = number % 100
20 
21     if remain >= 10 and remain <= 19:
22         spoken.append(SECOND_TEN[remain % 10])
23     else:
24         decade = remain / 10
25         if decade > 0:
26             spoken.append(OTHER_TENS[decade - 2])
27 
28         unit = remain % 10
29         if unit > 0:
30             spoken.append(FIRST_TEN[unit - 1])
31 
32     return ‘ ‘.join(spoken)

python有个divmod函数, 即可返回商又可返回余数h, number = divmod(number, 100)

可以如此构造字符串 final_string = "%s%s%s" (hundred_s, decade_s, unit_s)

使用strip去除字符,lstrip, rstrip; rstrip()去除右边空格

Speech Module,布布扣,bubuko.com

Speech Module

标签:blog   使用   io   for   cti   div   python   log   

原文地址:http://www.cnblogs.com/hzhesi/p/3891525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!