#-*-coding:utf-8-*- ''' 练习6-8:列表 给出一个整型值,返回符合英文语法规则的形式 例如输入89 返回的是 eight-nine 数字的大小在0~1000范围之内 ''' from __future__ import division import math def convert_num_eng(a_int): # 确定一个标准的输出列表内容 units_digit_list = [' ', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] tens_digit_list1 = [' ', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] tens_digit_list_num = [11, 12, 13, 14, 15, 16, 17, 18, 19] tens_digit_list2 = [ ' ','ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] hundred_str = 'hundred' a_out = ' ' if a_int/100 >= 1: # 该数大于等于100 a_out = units_digit_list[int(math.floor(a_int/100))] + ' ' + hundred_str if (a_int % 100)/10 >= 1: # 该数具有十位数字 if (a_int % 100) in tens_digit_list_num: # 判断是否处于 11~19 之间 a_out = a_out + ' and ' + tens_digit_list1[int((a_int % 100)/10)] else:# 大于19 a_out = a_out + ' and ' + tens_digit_list2[int(math.floor((a_int % 100) / 10))] + '-' + \ units_digit_list[int(math.floor((a_int % 100) % 10))] else: # 该数小于100 if (a_int / 10) >= 1: # 该数具有十位数字 if (a_int ) in tens_digit_list_num: # 判断是否处于 11~19 之间 a_out = a_out + tens_digit_list1[int(a_int % 10) ] else: # 大于19 a_out = a_out + tens_digit_list2[int(math.floor(a_int/ 10))] + '-' + \ units_digit_list[int(math.floor(a_int % 10))] else: # 仅仅个位数 a_out = units_digit_list[int(math.floor(a_int % 10))] print a_out return a_out if __name__ == '__main__': a_int = int(raw_input('请输入一个0~1000内的整数,我给你返回它的英文表达!!\n ')) english_out = convert_num_eng(a_int)
chapter 6 exercise 8
最新推荐文章于 2017-12-19 10:09:58 发布