From 15f2d8dcb8f30da7e4980e331c2912e4451f012f Mon Sep 17 00:00:00 2001 From: Eric G Butler Jr Date: Wed, 17 Sep 2025 21:19:01 -0500 Subject: [PATCH] =?UTF-8?q?Age=20Calculator:=20fix=20leap-year=20function?= =?UTF-8?q?=20and=20add=20time=20summary=20(days=E2=86=92hours/minutes/sec?= =?UTF-8?q?onds)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Age Calculator/calculate.py | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/Age Calculator/calculate.py b/Age Calculator/calculate.py index 83134f0..a5756bb 100644 --- a/Age Calculator/calculate.py +++ b/Age Calculator/calculate.py @@ -1,25 +1,25 @@ import time from calendar import isleap -def judge_leap_year(year): - if isleap(year): - return True - else: - return False +# Check if a year is a leap year +def judge_leap(year: int) -> bool: + return isleap(year) -def month_days(month, leap_year): +# Return number of days in a month, considering leap years +def month_days(month: int, leap_year: bool) -> int: 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): + else: return 28 - +# User input name = input("Please enter your name: ") -age = input("Please enter your age: ") +age = int(input("Please enter your age: ")) + localtime = time.localtime(time.time()) year = int(age) @@ -29,16 +29,25 @@ def month_days(month, leap_year): begin_year = int(localtime.tm_year) - year end_year = begin_year + year +# Count days in past years for y in range(begin_year, end_year): - if (judge_leap_year(y)): - day = day + 366 + if judge_leap(y): + day += 366 else: - day = day + 365 + day += 365 -leap_year = judge_leap_year(localtime.tm_year) +# Add days from current year +leap_year = judge_leap(localtime.tm_year) for m in range(1, localtime.tm_mon): - day = day + month_days(m, leap_year) - -day = day + localtime.tm_mday -print("\n\t%s's age is %d years or " % (name, year), end="") -print("%d months or %d days" % (month, day)) + day += month_days(m, leap_year) + +# Approximate breakdown (ignores time of day) +hours = day * 24 +minutes = hours * 60 +seconds = minutes * 60 + +print(f"\nHello {name}, you are approximately:") +print(f" {day:,} days") +print(f" {hours:,} hours") +print(f" {minutes:,} minutes") +print(f" {seconds:,} seconds old!")