Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 12b0bd5

Browse files
committed
Add modifiy code in each module
1 parent 8410af9 commit 12b0bd5

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import time
2+
from utilize_date import judge_leap_year, month_days
3+
4+
5+
def age_calculator(name, age):
6+
localtime = time.localtime(time.time())
7+
8+
year = int(age)
9+
month = year * 12 + localtime.tm_mon
10+
day = 0
11+
12+
begin_year = int(localtime.tm_year) - year
13+
end_year = begin_year + year
14+
15+
for y in range(begin_year, end_year):
16+
if judge_leap_year(y):
17+
day += 366
18+
else:
19+
day += 365
20+
21+
leap_year = judge_leap_year(localtime.tm_year)
22+
for m in range(1, localtime.tm_mon):
23+
day += month_days(m, leap_year)
24+
25+
day += localtime.tm_mday
26+
27+
return f"{name}'s age is {year} years or {month} months or {day} days"
28+
29+
30+
31+

projects/Calculate Age/src/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from calculate_age import age_calculator
2+
3+
4+
def main():
5+
input_name = input("input your name: ")
6+
7+
while True:
8+
input_age = input("input your age: ")
9+
try:
10+
string_to_int_age = int(input_age)
11+
if string_to_int_age <= 0:
12+
print("Please input a positive number.")
13+
else:
14+
break
15+
except ValueError:
16+
print("Please input a valid age.")
17+
18+
result = age_calculator(input_name, string_to_int_age)
19+
20+
print(result)
21+
22+
if __name__ == "__main__":
23+
main()
24+
25+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from calendar import isleap
2+
3+
4+
def judge_leap_year(year):
5+
if isleap(year):
6+
return True
7+
else:
8+
return False
9+
10+
11+
def month_days(month, leap_year):
12+
if month in [1, 3, 5, 7, 8, 10, 12]:
13+
return 31
14+
elif month in [4, 6, 9, 11]:
15+
return 30
16+
elif month == 2 and leap_year:
17+
return 29
18+
elif month == 2 and (not leap_year):
19+
return 28

0 commit comments

Comments
 (0)