码迷,mamicode.com
首页 > 编程语言 > 详细

python 键盘输入

时间:2017-12-27 11:53:21      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:mon   语言   usr   inpu   ber   编程   键盘输入   end   odi   

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

 

python 键盘输入

标签:mon   语言   usr   inpu   ber   编程   键盘输入   end   odi   

原文地址:https://www.cnblogs.com/zhangyating/p/8124503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!