-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #752 from Chloe23077/feature-branch
Calculate Age Project: Modularize Code and Fix Input Handling
- Loading branch information
Showing
22 changed files
with
646 additions
and
299 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import time | ||
from utilize_date import judge_leap_year, month_days | ||
|
||
|
||
def age_calculator(name, age): | ||
""" | ||
Calculate user's age in years, month, and days | ||
based on current date and print. | ||
Args: | ||
name (str): user's name. | ||
age (int): user's age in years. | ||
Returns: | ||
None. | ||
""" | ||
localtime = time.localtime(time.time()) | ||
|
||
year = int(age) | ||
month = year * 12 + localtime.tm_mon | ||
day = 0 | ||
|
||
begin_year = int(localtime.tm_year) - year | ||
end_year = begin_year + year | ||
|
||
for y in range(begin_year, end_year): | ||
if judge_leap_year(y): | ||
day += 366 | ||
else: | ||
day += 365 | ||
|
||
leap_year = judge_leap_year(localtime.tm_year) | ||
for m in range(1, localtime.tm_mon): | ||
day += month_days(m, leap_year) | ||
|
||
day += localtime.tm_mday | ||
|
||
return f"{name}'s age is {year} years or {month} months or {day} days" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from calculate_age import age_calculator | ||
|
||
|
||
def main(): | ||
""" | ||
the user to input name and age, validate input, | ||
and calculates and displays the user age in years, months and days. | ||
Args: | ||
None. | ||
Return: | ||
None. | ||
""" | ||
input_name = input("input your name: ") | ||
|
||
while True: | ||
input_age = input("input your age: ") | ||
try: | ||
string_to_int_age = int(input_age) | ||
if string_to_int_age <= 0: | ||
print("Please input a positive number.") | ||
else: | ||
break | ||
except ValueError: | ||
print("Please input a valid age.") | ||
|
||
result = age_calculator(input_name, string_to_int_age) | ||
|
||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from calendar import isleap | ||
|
||
|
||
def judge_leap_year(year): | ||
""" | ||
judge the leap year. | ||
Args: | ||
year (int): To check the year. | ||
return: | ||
bool: Ture if the year is a leap year, False otherwise. | ||
""" | ||
if isleap(year): | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def month_days(month, leap_year): | ||
""" | ||
Returns the number of days in each month | ||
Args: | ||
month (int): The month 1-12. | ||
Returns: | ||
int: The number of days in the month. | ||
""" | ||
if month in [1, 3, 5, 7, 8, 10, 12]: | ||
return 31 | ||
elif month in [4, 6, 9, 11]: | ||
return 30 | ||
elif month == 2 and leap_year: | ||
return 29 | ||
elif month == 2 and (not leap_year): | ||
return 28 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from unittest.mock import patch | ||
from calculate_age import age_calculator | ||
|
||
|
||
@patch("time.time", return_value=1621848668.0) | ||
def test_age_calculator(mock_time): | ||
""" | ||
Test the age_calculator function | ||
Mocks the current time and check if the age is calculated | ||
based on current time | ||
""" | ||
name = "Chloe" | ||
age = 30 | ||
expect_output = "Chloe's age is 30 years or 365 months or 11102 days" | ||
assert age_calculator(name, age) == expect_output | ||
|
||
|
||
def test_age_calculator_negative_age(): | ||
""" | ||
Tests the age_calculator function for negative age input | ||
Check for ValueError when user input negative age. | ||
""" | ||
name = "Emma" | ||
age = -5 | ||
try: | ||
age_calculator(name, age) | ||
except ValueError as e: | ||
assert str(e) == "Please input a positive number." | ||
|
||
|
||
@patch("time.time", return_value=1621848668.0) | ||
def test_age_calculator_leap_year(mock_time): | ||
""" | ||
Test the age_calculator function considering leap years | ||
Check for expect_output_leap_year comparing the real output considering leap years | ||
""" | ||
name = "David" | ||
age = 30 | ||
expect_output_leap_year = "David's age is 30 years or 365 months or 11102 days" | ||
assert age_calculator(name, age) == expect_output_leap_year |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytest | ||
from utilize_date import judge_leap_year, month_days | ||
|
||
|
||
def test_judge_leap_year(): | ||
""" | ||
judge_leap_year function tests whether a given year is a leap year. | ||
A leap year is divisible by 4 and, if divisible by 100, must also be divisible by 400. | ||
""" | ||
assert judge_leap_year(2000) == True | ||
assert judge_leap_year(2008) == True | ||
assert judge_leap_year(2023) == False | ||
assert judge_leap_year(1900) == False | ||
assert judge_leap_year(2400) == True | ||
assert judge_leap_year(2100) == False | ||
|
||
|
||
def test_month_days(): | ||
""" | ||
The month_days function tests whether if returns the correct number of days for a given month | ||
For Feb, both leap years and common years are considered. | ||
""" | ||
assert month_days(7, False) == 31 | ||
assert month_days(4, True) == 30 | ||
assert month_days(2, True) == 29 | ||
assert month_days(2, False) == 28 | ||
assert month_days(1, False) == 31 | ||
assert month_days(11, True) == 30 | ||
|
||
|
||
# "pytest -s test_utilize_date.py" to test this file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.