zoukankan      html  css  js  c++  java
  • 输入数字形式的年月日,输出英文形式。

    将以数指定年、月、日的日期打印出来

    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']

  • 相关阅读:
    SpringBoot 部署【war】到服务器的tomcat
    SpringBoot 部署【jar】前后端分离(nginx)
    VM安装centos7
    nginx 入门
    《从零开始学习Mysql5.7》笔记
    架构师技术栈
    【读书笔记】小强升职记
    lambda 表达式
    【软考】信息资料
    flutter 获取状态栏高度
  • 原文地址:https://www.cnblogs.com/colinsu/p/13991943.html
Copyright © 2011-2022 走看看