根据用户输入的年、月、日,调整成日常日期格式,并计算用户的年龄,最后打印到屏幕上。
分析:
-
用year存放用户输入的年,用month存放月,用day存放日,用age存放年龄;
-
其中年,原样输出即可;
-
月有12个,用列表
months=['January','Feburary','March','April','May','June','July','August','September','October','November','December']
-
日有31个,用列表
endings=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st'],
其中1-3是first、second、third,
然后是17个[th],代表的意思是4-20:
fourth、fifth、sixth、seventh、eighth、ninth、tenth、eleventh、twelfth、thirteenth、fifteenth、sixteenth、seventeenth、eighteenth、nineteenth、twentieth
然后是21-23
twenty first、twenty second、twenty third
然后是24-30
twenty fourth、twenty fifth、twenty sixth、twenty seventh、twenty eighth、twenty ninth、thirtieth
最后是thirty first;
-
注意:
实际的月份为months[int[month]-1]
天为day+endings[int[day]-1];
年龄=2018-int(year)
具体代码如下:
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 months=['January','Feburary','March','April','May','June','July','August','September','October','November','December'] 4 endings=['st','nd','rd']+17*['th']+['st','nd','rd']+7*['th']+['st'] 5 year=raw_input('Year: ') 6 month=raw_input('MOnth(1-12):') 7 day=raw_input('Day(1-31): ') 8 month_number=int(month)-1 9 day_number=int(day)-110 age=2018-int(year)11 print months[month_number]+' '+day+endings[day_number]+'.'+year12 print "Your age is: "+str(age)