Speech Module

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()去除右边空格

posted @ 2014-08-05 09:35  哲人善思  阅读(230)  评论(0编辑  收藏  举报