python:根据录入年月日打印出日期

基于python3.6编写

#根据给定的年月日以数字形式打印出日期
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)

输出结果:

year:1991
month(1-12):3
day(1-31)15

March ,15th ,1991

猜你喜欢

转载自blog.csdn.net/sinat_33859977/article/details/80490029