Last active
February 27, 2024 17:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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