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
时间: 2024-12-16 20:53:01