Age Calculator

Calculate age from birth date.

Logic BuildingIntermediate
Logic Building
from datetime import date

# Take birth date
year = int(input("Enter birth year: "))
month = int(input("Enter birth month: "))
day = int(input("Enter birth day: "))

# Calculate age
today = date.today()
birth_date = date(year, month, day)
age = today.year - birth_date.year

# Adjust if birthday hasn't occurred this year
if (today.month, today.day) < (birth_date.month, birth_date.day):
    age -= 1

print(f"Age: {age} years")

Output

Enter birth year: 2000
Enter birth month: 5
Enter birth day: 15
Age: 24 years

Calculate age from birth date.

Key Concepts:

  • Get current date
  • Calculate year difference
  • Adjust if birthday not passed