Age Calculator without any built-in function
I have given both codes with using in built-in module and without using it.Please Share if found helpful
Code:
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 = int(input("Enter your current year"))
current_month = int(input("Enter your current month"))
current_day = int(input("Enter your current 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")
Approach #2: Using datetime module
Python provides datetime module to deal with all datetime related issues in python. Using datetime we can find the age by subtracting birth year from current year. Along with this, we need to focus on the birth month and birthday. For this, we check if current month and date are less than birth month and date. If yes subtract 1 from age, otherwise 0.
0 Comments
Post a Comment