5. Python 打印年月日

Author: Notus([email protected])
Create: 2019-02-12
Update: 2019-02-12

输入年月日, 以数字形式打印

Python 版本: 2.7.11

代码如下

# -*- coding: utf-8 -*-
'''
    根据给定的年月日以数字的形式打印出日期
    @Author: Notus([email protected])
    @Create: 2019-02-12
    @Update: 2019-02-12
'''

months = [
    'January', 
    'February', 
    'March', 
    'Apirl', 
    '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

运行结果

Year: 2017
Month(1-12): 3
Day(1-31): 4
March 4th. 2017

猜你喜欢

转载自www.cnblogs.com/leo1875/p/10363753.html