From e0e6fcc62e1d539a5d6c1efeaa0f772b87f007ae Mon Sep 17 00:00:00 2001 From: Sewmini Fernando <116245302+SewminiFernando22@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:50:50 +0530 Subject: [PATCH] Create CalculateAge --- CalculateAge | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 CalculateAge diff --git a/CalculateAge b/CalculateAge new file mode 100644 index 0000000..ba203fc --- /dev/null +++ b/CalculateAge @@ -0,0 +1,22 @@ +from datetime import datetime + +# Function to calculate age +def calculate_age(birth_date): + today = datetime.today() + age = today.year - birth_date.year + # Adjust for birth month and day + if (today.month, today.day) < (birth_date.month, birth_date.day): + age -= 1 + return age + +# Get user input +year = int(input("Enter your birth year (YYYY): ")) +month = int(input("Enter your birth month (MM): ")) +day = int(input("Enter your birth day (DD): ")) + +# Create a date object from the input +birth_date = datetime(year, month, day) + +# Calculate age and print result +age = calculate_age(birth_date) +print(f"You are {age} years old.")