you are a great python programmer.
you have been asked to write a program to calculate people's age.
The input should ask for the date of birth in year-month-day format.
The output should indicate the age, and using the current date as a reference, indicate how many months and days have passed since the person's birthday. You should also indicate how many months and days are left until your birthday.
You must put comments inside the program that indicate what each block and function does.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Here's a Python program that calculates a person's age and determines the number of months and days that have passed since their last birthday. It also calculates the number of months and days left until their next birthday.
python
import datetime
def calculate_age(birth_date):
"""
This function calculates the age of a person based on their birth date.
It takes the birth date as input in 'year-month-day' format and returns the age in years.
"""
today = datetime.date.today()
birth_year, birth_month, birth_day = map(int, birth_date.split('-'))
age = today.year - birth_year
# Check if the birthday has already occurred this year
if today.month < birth_month or (today.month == birth_month and today.day < birth_day):
age -= 1
return age
def calculate_months_and_days(age, birth_date):
"""
This function calculates the number of months and days that have passed since the last birthday,
and the number of months and days left until the next birthday.
It takes the age and birth date as input in 'year-month-day' format and returns two tuples.
The first tuple contains the number of months and days that have passed since the last birthday,
and the second tuple contains the number of months and days left until the next birthday.
"""
today = datetime.date.today()
birth_year, birth_month, birth_day = map(int, birth_date.split('-'))
# Calculate the date of the last birthday
last_birthday = datetime.date(today.year, birth_month, birth_day)
# Calculate the date of the next birthday
next_birthday = datetime.date(today.year + 1, birth_month, birth_day)
# Calculate the number of days that have passed since the last birthday
days_passed = (today - last_birthday).days
# Calculate the number of days left until the next birthday
days_left = (next_birthday - today).days
# Calculate the number of months and days that have passed since the last birthday
months_passed = days_passed // 30
days_passed %= 30
# Calculate the number of months and days left until the next birthday
months_left = days_left // 30
days_left %= 30
return (months_passed, days_passed), (months_left, days_left)
# Ask for the date of birth in 'year-month-day' format
birth