Take a birthday of a person and then calculate the age.
This code and solution is contributed by Programmer Karim.
Take the user birthday. Use the same method that you used in the previous problem.
Then, to calculate age, get the days and then divide the days by 365.
(This is not the perfect way to calculate age...Feel free to find better solutions)
This code is contributed by Programmer Karim...
fromdatetimeimportdatetimeimporttimedefcalculate_age(born): today=datetime.today() days= (today-born).daysage=days//365returnagedefget_user_birthday(): date_str=input("Enter your birth date in DD/MM/YYYY: ") try: birthday=datetime.strptime(date_str, "%d/%m/%Y") exceptTypeError: birthday=datetime.datetime(*(time.strptime(date_str, "%d/%m/%Y")[0:6])) returnbirthdaybirthday=get_user_birthday() age=calculate_age(birthday) print("Your age: ", age)
You used the datetime earlier. And then, calculated the days between the birth date and today’s date.
Then, divide the days by 365 to get the age!
tags: programming-hero
python
python3
problem-solving
programming
coding-challenge
interview
learn-python
python-tutorial
programming-exercises
programming-challenges
programming-fundamentals
programming-contest
python-coding-challenges
python-problem-solving