|
| 1 | +diff --git a/projects/Calculate Age/calculate.py b/projects/Calculate Age/calculate.py |
| 2 | +deleted file mode 100644 |
| 3 | +index a7dc34f..0000000 |
| 4 | +--- a/projects/Calculate Age/calculate.py |
| 5 | ++++ /dev/null |
| 6 | +@@ -1,49 +0,0 @@ |
| 7 | +-import time |
| 8 | +-from calendar import isleap |
| 9 | +- |
| 10 | +- |
| 11 | +-# judge the leap year |
| 12 | +-def judge_leap_year(year): |
| 13 | +- if isleap(year): |
| 14 | +- return True |
| 15 | +- else: |
| 16 | +- return False |
| 17 | +- |
| 18 | +- |
| 19 | +-# returns the number of days in each month |
| 20 | +-def month_days(month, leap_year): |
| 21 | +- if month in [1, 3, 5, 7, 8, 10, 12]: |
| 22 | +- return 31 |
| 23 | +- elif month in [4, 6, 9, 11]: |
| 24 | +- return 30 |
| 25 | +- elif month == 2 and leap_year: |
| 26 | +- return 29 |
| 27 | +- elif month == 2 and (not leap_year): |
| 28 | +- return 28 |
| 29 | +- |
| 30 | +- |
| 31 | +-name = input("input your name: ") |
| 32 | +-age = input("input your age: ") |
| 33 | +-localtime = time.localtime(time.time()) |
| 34 | +- |
| 35 | +-year = int(age) |
| 36 | +-month = year * 12 + localtime.tm_mon |
| 37 | +-day = 0 |
| 38 | +- |
| 39 | +-begin_year = int(localtime.tm_year) - year |
| 40 | +-end_year = begin_year + year |
| 41 | +- |
| 42 | +-# calculate the days |
| 43 | +-for y in range(begin_year, end_year): |
| 44 | +- if judge_leap_year(y): |
| 45 | +- day = day + 366 |
| 46 | +- else: |
| 47 | +- day = day + 365 |
| 48 | +- |
| 49 | +-leap_year = judge_leap_year(localtime.tm_year) |
| 50 | +-for m in range(1, localtime.tm_mon): |
| 51 | +- day = day + month_days(m, leap_year) |
| 52 | +- |
| 53 | +-day = day + localtime.tm_mday |
| 54 | +-print("%s's age is %d years or " % (name, year), end="") |
| 55 | +-print("%d months or %d days" % (month, day)) |
| 56 | +diff --git a/projects/Calculate Age/src/__init__.py b/projects/Calculate Age/src/__init__.py |
| 57 | +new file mode 100644 |
| 58 | +index 0000000..e69de29 |
| 59 | +diff --git a/projects/Calculate Age/src/calculate_age.py b/projects/Calculate Age/src/calculate_age.py |
| 60 | +new file mode 100644 |
| 61 | +index 0000000..193fb64 |
| 62 | +--- /dev/null |
| 63 | ++++ b/projects/Calculate Age/src/calculate_age.py |
| 64 | +@@ -0,0 +1,42 @@ |
| 65 | ++import time |
| 66 | ++from utilize_date import judge_leap_year, month_days |
| 67 | ++ |
| 68 | ++ |
| 69 | ++def age_calculator(name, age): |
| 70 | ++ """ |
| 71 | ++ Calculate user's age in years, month, and days |
| 72 | ++ based on current date and print. |
| 73 | ++ |
| 74 | ++ Args: |
| 75 | ++ name (str): user's name. |
| 76 | ++ age (int): user's age in years. |
| 77 | ++ |
| 78 | ++ Returns: |
| 79 | ++ None. |
| 80 | ++ """ |
| 81 | ++ localtime = time.localtime(time.time()) |
| 82 | ++ |
| 83 | ++ year = int(age) |
| 84 | ++ month = year * 12 + localtime.tm_mon |
| 85 | ++ day = 0 |
| 86 | ++ |
| 87 | ++ begin_year = int(localtime.tm_year) - year |
| 88 | ++ end_year = begin_year + year |
| 89 | ++ |
| 90 | ++ for y in range(begin_year, end_year): |
| 91 | ++ if judge_leap_year(y): |
| 92 | ++ day += 366 |
| 93 | ++ else: |
| 94 | ++ day += 365 |
| 95 | ++ |
| 96 | ++ leap_year = judge_leap_year(localtime.tm_year) |
| 97 | ++ for m in range(1, localtime.tm_mon): |
| 98 | ++ day += month_days(m, leap_year) |
| 99 | ++ |
| 100 | ++ day += localtime.tm_mday |
| 101 | ++ |
| 102 | ++ return f"{name}'s age is {year} years or {month} months or {day} days" |
| 103 | ++ |
| 104 | ++ |
| 105 | ++ |
| 106 | ++ |
| 107 | +diff --git a/projects/Calculate Age/src/main.py b/projects/Calculate Age/src/main.py |
| 108 | +new file mode 100644 |
| 109 | +index 0000000..b775235 |
| 110 | +--- /dev/null |
| 111 | ++++ b/projects/Calculate Age/src/main.py |
| 112 | +@@ -0,0 +1,35 @@ |
| 113 | ++from calculate_age import age_calculator |
| 114 | ++ |
| 115 | ++ |
| 116 | ++def main(): |
| 117 | ++ """ |
| 118 | ++ the user to input name and age, validate input, |
| 119 | ++ and calculates and displays the user age in years, months and days. |
| 120 | ++ |
| 121 | ++ Args: |
| 122 | ++ None. |
| 123 | ++ |
| 124 | ++ Return: |
| 125 | ++ None. |
| 126 | ++ """ |
| 127 | ++ input_name = input("input your name: ") |
| 128 | ++ |
| 129 | ++ while True: |
| 130 | ++ input_age = input("input your age: ") |
| 131 | ++ try: |
| 132 | ++ string_to_int_age = int(input_age) |
| 133 | ++ if string_to_int_age <= 0: |
| 134 | ++ print("Please input a positive number.") |
| 135 | ++ else: |
| 136 | ++ break |
| 137 | ++ except ValueError: |
| 138 | ++ print("Please input a valid age.") |
| 139 | ++ |
| 140 | ++ result = age_calculator(input_name, string_to_int_age) |
| 141 | ++ |
| 142 | ++ print(result) |
| 143 | ++ |
| 144 | ++if __name__ == "__main__": |
| 145 | ++ main() |
| 146 | ++ |
| 147 | ++ |
| 148 | +diff --git a/projects/Calculate Age/src/utilize_date.py b/projects/Calculate Age/src/utilize_date.py |
| 149 | +new file mode 100644 |
| 150 | +index 0000000..8d8b129 |
| 151 | +--- /dev/null |
| 152 | ++++ b/projects/Calculate Age/src/utilize_date.py |
| 153 | +@@ -0,0 +1,36 @@ |
| 154 | ++from calendar import isleap |
| 155 | ++ |
| 156 | ++ |
| 157 | ++def judge_leap_year(year): |
| 158 | ++ """ |
| 159 | ++ judge the leap year. |
| 160 | ++ |
| 161 | ++ Args: |
| 162 | ++ year (int): To check the year. |
| 163 | ++ return: |
| 164 | ++ bool: Ture if the year is a leap year, False otherwise. |
| 165 | ++ """ |
| 166 | ++ if isleap(year): |
| 167 | ++ return True |
| 168 | ++ else: |
| 169 | ++ return False |
| 170 | ++ |
| 171 | ++ |
| 172 | ++def month_days(month, leap_year): |
| 173 | ++ """ |
| 174 | ++ Returns the number of days in each month |
| 175 | ++ |
| 176 | ++ Args: |
| 177 | ++ month (int): The month 1-12. |
| 178 | ++ |
| 179 | ++ Returns: |
| 180 | ++ int: The number of days in the month. |
| 181 | ++ """ |
| 182 | ++ if month in [1, 3, 5, 7, 8, 10, 12]: |
| 183 | ++ return 31 |
| 184 | ++ elif month in [4, 6, 9, 11]: |
| 185 | ++ return 30 |
| 186 | ++ elif month == 2 and leap_year: |
| 187 | ++ return 29 |
| 188 | ++ elif month == 2 and (not leap_year): |
| 189 | ++ return 28 |
| 190 | +diff --git a/projects/Calculate Age/tests/__init__.py b/projects/Calculate Age/tests/__init__.py |
| 191 | +new file mode 100644 |
| 192 | +index 0000000..e69de29 |
| 193 | +diff --git a/projects/Calculate Age/tests/test_calculate_age.py b/projects/Calculate Age/tests/test_calculate_age.py |
| 194 | +new file mode 100644 |
| 195 | +index 0000000..af0c07b |
| 196 | +--- /dev/null |
| 197 | ++++ b/projects/Calculate Age/tests/test_calculate_age.py |
| 198 | +@@ -0,0 +1,25 @@ |
| 199 | ++import time |
| 200 | ++from unittest.mock import patch |
| 201 | ++from calculate_age import age_calculator |
| 202 | ++ |
| 203 | ++@patch('time.time', return_value=1621848668.0) |
| 204 | ++def test_age_calculator(mock_time): |
| 205 | ++ name = "Chloe" |
| 206 | ++ age = 30 |
| 207 | ++ expect_output = "Chloe's age is 30 years or 365 months or 11102 days" |
| 208 | ++ assert age_calculator(name, age) == expect_output |
| 209 | ++ |
| 210 | ++def test_age_calculator_negative_age(): |
| 211 | ++ name = "Emma" |
| 212 | ++ age = -5 |
| 213 | ++ try: |
| 214 | ++ age_calculator(name, age) |
| 215 | ++ except ValueError as e: |
| 216 | ++ assert str(e) == "Please input a positive number." |
| 217 | ++ |
| 218 | ++ |
| 219 | ++def test_age_calculator_leap_year(): |
| 220 | ++ name = "David" |
| 221 | ++ age = 30 |
| 222 | ++ expect_output_leap_year = "David's age is 30 years or 365 months or 11100 days" |
| 223 | ++ assert age_calculator(name, age) == expect_output_leap_year |
| 224 | +diff --git a/projects/Calculate Age/test_calculate.py b/projects/Calculate Age/tests/test_utilize_date.py |
| 225 | +similarity index 83% |
| 226 | +rename from projects/Calculate Age/test_calculate.py |
| 227 | +rename to projects/Calculate Age/tests/test_utilize_date.py |
| 228 | +index 369ac65..54e5efa 100644 |
| 229 | +--- a/projects/Calculate Age/test_calculate.py |
| 230 | ++++ b/projects/Calculate Age/tests/test_utilize_date.py |
| 231 | +@@ -1,5 +1,6 @@ |
| 232 | + import pytest |
| 233 | +-from calculate import judge_leap_year, month_days |
| 234 | ++from utilize_date import judge_leap_year, month_days |
| 235 | ++ |
| 236 | + |
| 237 | + def test_judge_leap_year(): |
| 238 | + assert judge_leap_year(2000) == True |
| 239 | +@@ -9,6 +10,7 @@ def test_judge_leap_year(): |
| 240 | + assert judge_leap_year(2400) == True |
| 241 | + assert judge_leap_year(2100) == False |
| 242 | + |
| 243 | ++ |
| 244 | + def test_month_days(): |
| 245 | + assert month_days(7, False) == 31 |
| 246 | + assert month_days(4, True) == 30 |
| 247 | +@@ -17,4 +19,5 @@ def test_month_days(): |
| 248 | + assert month_days(1, False) == 31 |
| 249 | + assert month_days(11, True) == 30 |
| 250 | + |
| 251 | +-# "pytest -s test_calculate.py" to test this file |
| 252 | +\ No newline at end of file |
| 253 | ++ |
| 254 | ++# "pytest -s test_utilize_date.py" to test this file |
0 commit comments