Skip to content

Instantly share code, notes, and snippets.

@MahraibFatima
Last active February 27, 2024 17:17
Show Gist options
  • Save MahraibFatima/d59566cf0a243db00939ca859d7b8a9f to your computer and use it in GitHub Desktop.
Save MahraibFatima/d59566cf0a243db00939ca859d7b8a9f to your computer and use it in GitHub Desktop.
A program where a user enters his date of birth as an input, and the application gives his age as an output.
import datetime
def get_valid_date(prompt):
while True:
try:
date_str = input(prompt)
date = datetime.datetime.strptime(date_str, '%Y-%m-%d').date()
return date
except ValueError:
print("Please enter a valid date in the format YYYY-MM-DD.")
def ageCalculator():
dob = get_valid_date("Enter your date of birth (YYYY-MM-DD): ")
today = datetime.datetime.now().date()
age = (today - dob) // datetime.timedelta(days=365)
return age
print("Your age is:", ageCalculator())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment