当前位置: 移动技术网 > IT编程>脚本编程>Python > python实现打印年、月、日的日期

python实现打印年、月、日的日期

2020年07月07日  | 移动技术网IT编程  | 我要评论

本博文源于《Python基础教程》,旨在探讨如何实现打印年、月、日的日期。

实验效果

分别输入年月日,最后将月份转化为英文状态,日期加上后缀
在这里插入图片描述

实验原理

月份1-12存放进列表,根据用户输入的数组,当作列表的索引,取出英文状态月份。
日期就是按照英文状态生成31个。

实验代码

months = ['January','February','March','April','May',
          'June','July','August','September','October','November','December']
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)

month_name = months[month_number-1]
ordinal = day + endings[day_number-1]

print(month_name + ' ' + ordinal + ', ' + year)


本文地址:https://blog.csdn.net/m0_37149062/article/details/107142717

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网