1
+ import time
2
+ from calendar import isleap
3
+
4
+ # judge the leap year
5
+ def judge_leap_year (year ):
6
+ if isleap (year ):
7
+ return True
8
+ else :
9
+ return False
10
+
11
+
12
+ # returns the number of days in each month
13
+ def month_days (month , leap_year ):
14
+ if month in [1 , 3 , 5 , 7 , 8 , 10 , 12 ]:
15
+ return 31
16
+ elif month in [4 , 6 , 9 , 11 ]:
17
+ return 30
18
+ elif month == 2 and leap_year :
19
+ return 29
20
+ elif month == 2 and (not leap_year ):
21
+ return 28
22
+
23
+
24
+ name = input ("input your name: " )
25
+ age = input ("input your age: " )
26
+ localtime = time .localtime (time .time ())
27
+
28
+ year = int (age )
29
+ month = year * 12 + localtime .tm_mon
30
+ day = 0
31
+
32
+ begin_year = int (localtime .tm_year ) - year
33
+ end_year = begin_year + year
34
+
35
+ # calculate the days
36
+ for y in range (begin_year , end_year ):
37
+ if (judge_leap_year (y )):
38
+ day = day + 366
39
+ else :
40
+ day = day + 365
41
+
42
+ leap_year = judge_leap_year (localtime .tm_year )
43
+ for m in range (1 , localtime .tm_mon ):
44
+ day = day + month_days (m , leap_year )
45
+
46
+ day = day + localtime .tm_mday
47
+ print ("%s's age is %d years or " % (name , year ), end = "" )
48
+ print ("%d months or %d days" % (month , day ))
0 commit comments