1
1
import time
2
2
from calendar import isleap
3
3
4
- def judge_leap_year (year ):
5
- if isleap (year ):
6
- return True
7
- else :
8
- return False
4
+ # Check if a year is a leap year
5
+ def judge_leap (year : int ) -> bool :
6
+ return isleap (year )
9
7
10
- def month_days (month , leap_year ):
8
+ # Return number of days in a month, considering leap years
9
+ def month_days (month : int , leap_year : bool ) -> int :
11
10
if month in [1 , 3 , 5 , 7 , 8 , 10 , 12 ]:
12
11
return 31
13
12
elif month in [4 , 6 , 9 , 11 ]:
14
13
return 30
15
14
elif month == 2 and leap_year :
16
15
return 29
17
- elif month == 2 and ( not leap_year ) :
16
+ else :
18
17
return 28
19
18
20
-
19
+ # User input
21
20
name = input ("Please enter your name: " )
22
- age = input ("Please enter your age: " )
21
+ age = int (input ("Please enter your age: " ))
22
+
23
23
localtime = time .localtime (time .time ())
24
24
25
25
year = int (age )
@@ -29,16 +29,25 @@ def month_days(month, leap_year):
29
29
begin_year = int (localtime .tm_year ) - year
30
30
end_year = begin_year + year
31
31
32
+ # Count days in past years
32
33
for y in range (begin_year , end_year ):
33
- if ( judge_leap_year ( y ) ):
34
- day = day + 366
34
+ if judge_leap ( y ):
35
+ day += 366
35
36
else :
36
- day = day + 365
37
+ day += 365
37
38
38
- leap_year = judge_leap_year (localtime .tm_year )
39
+ # Add days from current year
40
+ leap_year = judge_leap (localtime .tm_year )
39
41
for m in range (1 , localtime .tm_mon ):
40
- day = day + month_days (m , leap_year )
41
-
42
- day = day + localtime .tm_mday
43
- print ("\n \t %s's age is %d years or " % (name , year ), end = "" )
44
- print ("%d months or %d days" % (month , day ))
42
+ day += month_days (m , leap_year )
43
+
44
+ # Approximate breakdown (ignores time of day)
45
+ hours = day * 24
46
+ minutes = hours * 60
47
+ seconds = minutes * 60
48
+
49
+ print (f"\n Hello { name } , you are approximately:" )
50
+ print (f" { day :,} days" )
51
+ print (f" { hours :,} hours" )
52
+ print (f" { minutes :,} minutes" )
53
+ print (f" { seconds :,} seconds old!" )
0 commit comments