索引脚本实例1

 根据给定的年月日以数字的形式打印日期
1
#!/usr/bin/env python 2 #encoding: utf-8 3 months = [ 4 'January', 5 'February', 6 'March', 7 'April', 8 'May', 9 'June', 10 'July', 11 'August', 12 'September', 13 'Otober', 14 'November', 15 'December' 16 ] 17 #以1-31的数字作为结尾的列表 18 endings = ['st','nd','rd'] + 17 * ['th'] \ 19 + ['st','nd','rd'] + 7 * ['th'] \ 20 + ['st'] 21 year = raw_input('year: ') 22 month = raw_input('Month(1-12): ') 23 day = raw_input('Day(1-31): ') 24 25 month_number = int(month) 26 day_number = int(day) 27 28 #记得要将月份和天数减1,以获得正确的索引 29 month_name = months[month_number - 1] 30 ordinal = day + endings[day_number - 1] 31 32 print month_name + ' ' + ordinal + ',' + year

执行结果:
[root@localhost script]# python 1.py
year: 1942
Month(1-12): 12
Day(1-31): 1
December 1st,1942

 



 

posted @ 2017-10-19 01:03  明王不动心  阅读(264)  评论(0编辑  收藏  举报