python键盘输入与其他编程语言基本类似,回车键结束输入
下面来看一段键盘输入年月日,英文输出示例:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 # 定义英文月份 4 months = ["january", "February", "March", "April", "May", "June", "july", "August", "September", 5 "October", "November", "December"] 6 # 定义天数结尾 7 endings = ["st", "nd", "rd"] + 17 * ["th"] 8 + ["st", "nd", "rd"] + 7 * ["th"] 9 + ["th"] 10 # 输入年月日 11 year = input("year:") 12 month = input("month(1-12):") 13 day = input("day(1-31):") 14 # 将月份和日期转换为整型 15 month_number = int(month) 16 day_number = int(day) 17 # 获取年月日并输出 18 month_name = months[month_number - 1] 19 ordinal = day + endings[day_number - 1] 20 print(month_name + ‘ ‘ + ordinal + ‘ ‘ + year)
输出结果:
1 year:2017 2 month(1-12):2 3 day(1-31):20 4 February 20th 2017