From 8dd452706338eb73c72cdf32ab3d5dc819283420 Mon Sep 17 00:00:00 2001 From: Kashish Juneja <69794677+KashishJuneja101003@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:16:36 +0530 Subject: [PATCH 1/3] Updated age.py to improve age calculation logic It is a complete different code than age.py --- .../Python/Age calculator/Updated age.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Desktop Application/Basic/Python/Age calculator/Updated age.py diff --git a/Desktop Application/Basic/Python/Age calculator/Updated age.py b/Desktop Application/Basic/Python/Age calculator/Updated age.py new file mode 100644 index 000000000..2d96991da --- /dev/null +++ b/Desktop Application/Basic/Python/Age calculator/Updated age.py @@ -0,0 +1,40 @@ +import datetime + +# Get the current system date and time +now = datetime.datetime.now() + +try: + # Take user input of their birth date + birthday = input("When is your birthday? (dd/mm/yyyy): ") + birthday = datetime.datetime.strptime( + birthday, + "%d/%m/%Y", + ).date() + + # Calculate age + age_years = now.year - birthday.year + age_months = now.month - birthday.month + age_days = now.day - birthday.day + + if(age_days < 0): + age_months -= 1 + + '''1. If the age_days is negative, it implies the month must be one less than the month number that is calculated and stored in age_months + 2. "(now.month-1)%12 or 12" ensures that when the previous month is calculated, its value lie in the range [1, 12] + ''' + previous_month = (now.month - 1) % 12 or 12 + + days_in_prev_month = (datetime.date(now.year, previous_month + 1, 1) - datetime.date(now.year, previous_month, 1)).days + + age_days += days_in_prev_month + + if (age_months < 0): + '''If age_months is negative, it implies the year must be one less than the year number that is calculated and store in age_years''' + age_years -= 1 + age_months += 12 + + # Printing the current age of the user + print(f"Your birthday is {birthday.strftime('%d %B %Y')}\nYour current age is {age_years} years, {age_months} months and {age_days} days.") + +except ValueError: + print("Either the date or the format is invalid.") \ No newline at end of file From 1609e34cbf2123eed69e9a998847df541cbb6037 Mon Sep 17 00:00:00 2001 From: Kashish Juneja <69794677+KashishJuneja101003@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:24:08 +0530 Subject: [PATCH 2/3] Updated age.py --- .../Basic/Python/Age calculator/age.py | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/Desktop Application/Basic/Python/Age calculator/age.py b/Desktop Application/Basic/Python/Age calculator/age.py index ba3fb439b..46a7a792a 100644 --- a/Desktop Application/Basic/Python/Age calculator/age.py +++ b/Desktop Application/Basic/Python/Age calculator/age.py @@ -1,33 +1,40 @@ import datetime + +# Get the current system date and time now = datetime.datetime.now() -key1 = now.year - 18 -key3 = now.month -d1 = datetime.date.today() -num = 0 + try: - birthday = input("When is your birthday?") - birthday = datetime.datetime.strptime(birthday,"%d/%m/%Y",).date() - key2 = birthday.year+18 - age = now.year - birthday.year - b_month = birthday.month - n_year = now.year - b_year = birthday.year -###################################################################################################################################################################### - while b_year < n_year: - year1 = 12 # 1 year = 12 months - num = num + 1 - b_year = b_year + 1 - num2 = 12 - b_month - months = num * 12 + num2 - 1 - days = now.day - birthday.day - # print(days) -####################################################################################################################################################################### - if birthday <= datetime.date.today() and key2 <= now.year: - print("Your birthday is "+ birthday.strftime('%d, %B %Y'), "and Your Age is", age, "years and", num2-1,"months and", days, "days") - elif age < 0: - print("Date or format is incorrect") - else: - print("You are under 18", "and Your Age is", age, "years and", num2-1,"months and", days, "days") -except: - print("date or format is invalid") -25 + # Take user input of their birth date + birthday = input("When is your birthday? (dd/mm/yyyy): ") + birthday = datetime.datetime.strptime( + birthday, + "%d/%m/%Y", + ).date() + + # Calculate age + age_years = now.year - birthday.year + age_months = now.month - birthday.month + age_days = now.day - birthday.day + + if(age_days < 0): + age_months -= 1 + + '''1. If the age_days is negative, it implies the month must be one less than the month number that is calculated and stored in age_months + 2. "(now.month-1)%12 or 12" ensures that when the previous month is calculated, its value lie in the range [1, 12] + ''' + previous_month = (now.month - 1) % 12 or 12 + + days_in_prev_month = (datetime.date(now.year, previous_month + 1, 1) - datetime.date(now.year, previous_month, 1)).days + + age_days += days_in_prev_month + + if (age_months < 0): + '''If age_months is negative, it implies the year must be one less than the year number that is calculated and store in age_years''' + age_years -= 1 + age_months += 12 + + # Printing the current age of the user + print(f"Your birthday is {birthday.strftime('%d %B %Y')}\nYour current age is {age_years} years, {age_months} months and {age_days} days.") + +except ValueError: + print("Either the date or the format is invalid.") From d226155f9c0547aff65ccba16a23c5e552c81eea Mon Sep 17 00:00:00 2001 From: Kashish Juneja <69794677+KashishJuneja101003@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:24:48 +0530 Subject: [PATCH 3/3] Delete Desktop Application/Basic/Python/Age calculator/Updated age.py --- .../Python/Age calculator/Updated age.py | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 Desktop Application/Basic/Python/Age calculator/Updated age.py diff --git a/Desktop Application/Basic/Python/Age calculator/Updated age.py b/Desktop Application/Basic/Python/Age calculator/Updated age.py deleted file mode 100644 index 2d96991da..000000000 --- a/Desktop Application/Basic/Python/Age calculator/Updated age.py +++ /dev/null @@ -1,40 +0,0 @@ -import datetime - -# Get the current system date and time -now = datetime.datetime.now() - -try: - # Take user input of their birth date - birthday = input("When is your birthday? (dd/mm/yyyy): ") - birthday = datetime.datetime.strptime( - birthday, - "%d/%m/%Y", - ).date() - - # Calculate age - age_years = now.year - birthday.year - age_months = now.month - birthday.month - age_days = now.day - birthday.day - - if(age_days < 0): - age_months -= 1 - - '''1. If the age_days is negative, it implies the month must be one less than the month number that is calculated and stored in age_months - 2. "(now.month-1)%12 or 12" ensures that when the previous month is calculated, its value lie in the range [1, 12] - ''' - previous_month = (now.month - 1) % 12 or 12 - - days_in_prev_month = (datetime.date(now.year, previous_month + 1, 1) - datetime.date(now.year, previous_month, 1)).days - - age_days += days_in_prev_month - - if (age_months < 0): - '''If age_months is negative, it implies the year must be one less than the year number that is calculated and store in age_years''' - age_years -= 1 - age_months += 12 - - # Printing the current age of the user - print(f"Your birthday is {birthday.strftime('%d %B %Y')}\nYour current age is {age_years} years, {age_months} months and {age_days} days.") - -except ValueError: - print("Either the date or the format is invalid.") \ No newline at end of file