forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAge_Calculator.py
32 lines (24 loc) · 1.08 KB
/
Age_Calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from datetime import date
def calculate_age(birthday):
today = date.today()
# Check if the birthdate is in the future
if today < birthday:
return "Invalid birthdate. Please enter a valid date."
day_check = ((today.month, today.day) < (birthday.month, birthday.day))
year_diff = today.year - birthday.year - day_check
remaining_months = abs((12-birthday.month)+today.month)
remaining_days = abs(today.day - birthday.day)
# Return the age as a formatted string
age_string = f"Age: {year_diff} years, {remaining_months} months, and {remaining_days} days"
return age_string
if __name__ == "__main__":
print(" Age Calculator By Python")
try:
birthYear = int(input("Enter the birth year: "))
birthMonth = int(input("Enter the birth month: "))
birthDay = int(input("Enter the birth day: "))
dateOfBirth = date(birthYear, birthMonth, birthDay)
age = calculate_age(dateOfBirth)
print(age)
except ValueError:
print("Invalid input. Please enter valid integers for the year, month, and day.")