将以数指定年、月、日的日期打印出来
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 = input('Year: ')
month = input('Month (1-12): ')
day = 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)
注:17*[th] 生成 一个长度为17的列表,元素均为5261'th'。+,将列表合并成一个大的列表。所以这行代码的作用时生成一个长度为31的后缀字符数组。这样就可以在day数字(1-31)后面 相应地加上后缀。如(1st, 2nd, 3rd, 4th, 5th, … , 19th, 20th, 21st, 22nd, 22rd, 23rd, 24th, … , 30th, 31st)。
endings=['st','nd','rd']+17['th']+['st','nd','rd']+7['th']+['st']
print 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']