python核心编程第六章练习6-8

6-8.
列表。给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”。附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”。本练习中的值假定在0~1000。
【答案】
代码如下:

number = int(raw_input('Please input a number between 1 to 1000: ... '))

units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tens_units = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] print 'The number you input is: ' if 0 <= number <= 9: print units[number]
elif 10 <= number <= 19: print tens_units[number - 10]
elif 20 <= number <= 99:
if number % 10 == 0: print tens[number / 10 - 2]
else: print '%s-%s' % (tens[number / 10 - 2], units[number % 10])
elif 100 <= number <= 999:
if number % 100 == 0: print '%s hundred' % (units[number / 100])
elif number % 10 == 0 and (number - 100 * (number / 100)) != 10: print '%s hundred and %s' % (units[number / 100], tens[(number - 100 * (number / 100))/10 - 2])
elif number % 10 == 0 and (number - 100 * (number / 100)) == 10: print '%s hundred and ten' % (units[number / 100])
elif number % 10 != 0 and 20 < (number - 100 * (number / 100))<= 99: print '%s hundred and %s-%s' % (units[number / 100], tens[(number - 100 * (number / 100)) / 10 - 2], units[number - 10 * (number / 10)])
elif number % 10 != 0 and 10 < (number - 100 * (number / 100))< 20: print '%s hundred and %s' % (units[number / 100], tens_units[number - 100 * (number / 100) - 10])
elif number % 10 != 0 and 0 < (number - 10 * (number / 10)) < 10: print '%s hundred and %s' % (units[number / 100], units[number - 10 * (number / 10)])
else: print 'ten hundred'

  

【执行结果】

Please input a number between 1 to 1000: ... 309
The number you input is:
three hundred and nine Please input a number between 1 to 1000: ... 721
The number you input is:
seven hundred and twenty-one

  

看了一些网上的答案 发现没有自己做的那么简单易懂,算了自己做吧。下面是自己做的:

#!/usr/bin/env python

number = int(raw_input('Please input a number between 0 to 1000:'))
digit = ['zero','one','two','three','four','five','six','seven','eight','nine']
tendigit = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
units = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'] if 0 <= number <= 9:
print 'translate into english is:',digit[number]
if 10 <= number <= 19:
print 'translate into english is:',tendigit[number-10]
if 20 <= number <= 99:
print 'translate into english is:',units[int(number/10)-2],'-',digit[number%10]
if 100 <= number <= 999:
ten = number%100
if 0 <= ten <= 9:
print 'translate into english is:',digit[int(number/100)],'hundred and',digit[ten]
elif 10 <= ten <= 19:
print 'translate into english is:',digit[int(number/100)],'hundred and',tendigit[ten-10]
else:
print 'translate into english is:',digit[int(number/100)],'hundred and',units[int(ten/10)-2],'-',digit[ten%10]

  

测试:

Please input a number between 0 to 1000:907
translate into english is: nine hundred and seven

  

Please input a number between 0 to 1000:917
translate into english is: nine hundred and seventeen

  

上一篇:IoC原理及实现


下一篇:IOC模式及Unity框架文章收藏