1. 程式人生 > >python 鍵盤輸入

python 鍵盤輸入

其他 回車 bin utf 編程語言 ember Coding february log

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 鍵盤輸入