Untitled

 avatar
unknown
plain_text
a month ago
772 B
1
Indexable
from datetime import datetime

def calculate_age(birth_date):
    today = datetime.today()
    age = today.year - birth_date.year

    # Check if the birthday has occurred this year
    if (today.month, today.day) < (birth_date.month, birth_date.day):
        age -= 1

    return age

def main():
    # Get user input for birth date
    birth_date_input = input("Enter your birth date (YYYY-MM-DD): ")
    
    try:
        # Convert the input string to a datetime object
        birth_date = datetime.strptime(birth_date_input, "%Y-%m-%d")
        age = calculate_age(birth_date)
        print(f"You are {age} years old.")
    except ValueError:
        print("Invalid date format. Please use YYYY-MM-DD.")

if __name__ == "__main__":
    main()
Leave a Comment