#coding=utf_8
#注释中文必须加上第一行代码否则无法保存代码
#2016年11月9日17:52:00 by Nxtou
#根据给定的年月日以数字形式打印出日期
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
#以1-31的数字作为结尾的列表
endings = ['st','nd','rd']+17*['th']\
+['st','nd','rd']+7*['th']\
+['st']
year = raw_input('Year: ')
month = raw_input('Month (1-12): ')
day = raw_input('Day (1-31): ')
month_number = int(month)
day_number = int(day)
#记得要将月份和天数减1,以获得正确的索引
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print month_name+' '+ordinal+'.'+year
运行结果:
>>>
======================== RESTART: D:\Python27\2_1.py ========================
Year: 2016
Month (1-12): 11
Day (1-31): 9
November 9th.2016
代码解析:
endings = ['st','nd','rd']+17*['th']\
+['st','nd','rd']+7*['th']\
+['st']
起初看不懂这个17*[‘th’],7*[‘th’]到底是什么意思,上网查了下资料,可以直接endings一下看看里面内容
>>> endings
['st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st']
>>>
在根据代码看,这个17*[‘th’],就是很明了了