← Previous Module 1: Variables, Input/Output, and Basic Math Next →
Statement
Solution

Python Exercise 1.2: Age Calculator

Create a Python program that asks for the user's birth year and calculates their current age.

Your program should:

  • Ask the user for their Birth Year (integer).
  • Get the Current Year (e.g., 2025).
  • Calculate the Age by subtracting the birth year from the current year.
  • Print the result in a friendly sentence.

Bonus Challenge: Handle the case where they haven't had their birthday yet this year (requires asking for birth month/day). For this basic exercise, we will assume the birthday has passed.

Sample Interaction:

Input
Output
2000
Enter your birth year: 2000 -------------------- Current Year: 2025 You are approximately 25 years old.

Solution

Here is how you can solve this using the datetime module to get the real current year automatically.

import datetime # 1. Get User Input birth_year = int(input("Enter your birth year: ")) # 2. Get Current Year # date.today().year gives us the current year (e.g., 2025) current_year = datetime.date.today().year # 3. Calculate Age age = current_year - birth_year # 4. Print Output print("-" * 20) print(f"Current Year: {current_year}") print(f"You are approximately {age} years old.")

Key Concepts:

  • int() converts the input string to a number so we can do math.
  • import datetime allows us to work with real dates.
  • datetime.date.today().year fetches the system's current year dynamically.
Python 3
Test Console
Run code to see output...
Scroll to Top