from datetime import datetime
# 讀入生日與今天日期
birth_str = input("生日(yyyy-mm-dd):")
today_str = input("今天(yyyy-mm-dd):")
birth = datetime.strptime(birth_str, "%Y-%m-%d")
today = datetime.strptime(today_str, "%Y-%m-%d")
# 年齡計算(考慮是否過生日)
age = today.year - birth.year
if (today.month, today.day) < (birth.month, birth.day):
age -= 1
# 分類
def classify_age(age):
if 0 <= age <= 10:
return 1
elif age <= 17:
return 2
elif age <= 44:
return 3
elif age <= 64:
return 4
else:
return 5
print(classify_age(age))
PROBLEM
計算年齡
一題一題刷