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

Commit 98cec56

Browse files
committed
Add calculate.py and test_calculate.py
1 parent dda7eb4 commit 98cec56

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)