import datetime print("##### Welcome to age calculator ######") birth_year = int(input("Enter your year of birth: \n")) birth_month = int(input("Enter your month of birth: \n")) birth_day = int(input("Enter your day of birth: \n")) current_year = datetime.date.today().year current_month = datetime.date.today().month current_day = datetime.date.today().day age_year = current_year - birth_year age_month = abs(current_month-birth_month) age_day = abs(current_day-birth_day) print("Your exact age is: ", age_year, "Years", age_month, "months and", age_day, "days") Output should look like something: ##### Welcome to age calculator ###### Enter your year of birth: 1986 Enter your month of birth: 05 Enter your day of birth: 25 Your exact age is: 34 Years 2 months and 2 days
This simply doesn’t work. you will need to create loops to determine whether a person has crossed a specific month time frame. I just started learning python and created one just was looking for a much more efficient way.
from datetime import date
todays_date = date.today()
print(“Current date: “, todays_date)
print(“Current year:”, todays_date.year)
print(“Current month:”, todays_date.month)
print(“Current day:”, todays_date.day)
birth_date = int(input(“Enter The date of your birth:”))
birth_month = int(input(“Enter The month of your birth:”))
birth_Year = int(input(“Enter The year of your birth:”))
#we will create a variable dy that is the age that is determined only by knowing the year of birth
dy = todays_date.year – birth_Year
ldy = dy-1
if todays_date.month > birth_month:
print(f”you are {dy} years old”)
elif todays_date.month = birth_date:
print(f”you are {dy} years old”)
elif todays_date.day < birth_date:
print(f"you are {ldy} years old")
It’s just a basic code for calculating age. You can always add more to it.