DEV Community

Cover image for Day 26 of My 90 Days Python Series โ€“ Age Calculator ๐Ÿงฎ
momina raheel (Moona)
momina raheel (Moona)

Posted on

Day 26 of My 90 Days Python Series โ€“ Age Calculator ๐Ÿงฎ

๐Ÿš€ Day 26 of My 90 Days Python Series โ€“ Age Calculator ๐Ÿงฎ

Have you ever caught yourself mentally calculating your exact age and losing track halfway through? ๐Ÿ˜…

Well, not anymore.

Todayโ€™s project is a simple yet smart Python Age Calculator that instantly tells you your age in years and months โ€” no more guessing or counting on fingers!

This is part of my 90 Days Python Series, where I build small, practical projects every day to sharpen my skills. ๐Ÿโœจ


๐Ÿง  Why I Built This Project

Letโ€™s be honest โ€” calculating age isnโ€™t difficult, but remembering the math can be annoying.

And as a Python learner, I wanted a project that:

  • ๐Ÿงฎ Uses real-world logic
  • ๐Ÿง  Strengthens understanding of date & time handling
  • ๐Ÿ” Includes loops and conditions
  • โœ… Is beginner-friendly and easy to build

The Age Calculator ticks all those boxes!


โœจ What This Age Calculator Can Do

โœ… Takes your birthdate as input (in YYYY-MM-DD format)

โœ… Calculates your exact age in years and months

โœ… Handles invalid date inputs gracefully

โœ… Keeps running until you choose to exit

โœ… Simple terminal interface โ€” no extra installations needed!


๐Ÿง‘โ€๐Ÿ’ป Example Output


โœจ Welcome to the Age Calculator โœจ

Enter your birthdate (YYYY-MM-DD): 2002-08-14

๐ŸŽ‰ You are 23 years and 1 month old!

Do you want to calculate another age? (yes/no): yes

Enter your birthdate (YYYY-MM-DD): 1995-02-01

๐ŸŽ‰ You are 30 years and 8 months old!

Do you want to calculate another age? (yes/no): no

Thanks for using the Age Calculator! ๐Ÿงก

Enter fullscreen mode Exit fullscreen mode


`


โš™๏ธ How It Works โ€” Step by Step

  1. Import the datetime module
    • Pythonโ€™s datetime makes it super easy to work with dates.
  2. Ask for user input in the format YYYY-MM-DD.
  3. Convert the string into a date object using datetime.strptime.
  4. Compare the entered date with the current date to find the difference.
  5. Extract years and months from the difference.
  6. Use a while loop so the program keeps running until the user says โ€œno.โ€
  7. Use try / except to handle invalid formats gracefully.

๐Ÿงฉ Code Overview (Beginner Friendly)

Hereโ€™s the core logic of the Age Calculator ๐Ÿ‘‡

`python
from datetime import datetime

while True:
try:
birth_date_str = input("Enter your birthdate (YYYY-MM-DD): ")
birth_date = datetime.strptime(birth_date_str, "%Y-%m-%d")
today = datetime.today()

    # Calculate year and month difference
    years = today.year - birth_date.year
    months = today.month - birth_date.month

    # Adjust if the current month is before the birth month
    if months < 0:
        years -= 1
        months += 12

    print(f"\n๐ŸŽ‰ You are {years} years and {months} months old!\n")

except ValueError:
    print("โŒ Invalid format. Please use YYYY-MM-DD.\n")
    continue

again = input("Do you want to calculate another age? (yes/no): ").lower()
if again != "yes":
    print("\nThanks for using the Age Calculator! ๐Ÿงก")
    break
Enter fullscreen mode Exit fullscreen mode


๐Ÿง  Python Concepts Youโ€™ll Learn

  • ๐Ÿ“… datetime module
  • ๐Ÿงญ String parsing with strptime
  • ๐Ÿงฎ Date calculations
  • ๐ŸŒ€ while loops for continuous execution
  • ๐Ÿงฑ try / except for error handling
  • ๐Ÿง  Conditional logic for month adjustment

This is a perfect mini project to strengthen your grasp of working with real-world data in Python.


๐Ÿ“‚ GitHub Repository

You can explore the complete project here:
๐Ÿ‘‰ https://github.com/shadowMomina/day-26-age-calculator.git

Feel free to โญ the repo if you find it helpful!


๐ŸŒŸ Why Mini Projects Like This Matter

Learning Python isnโ€™t just about reading tutorials โ€” itโ€™s about building something every day.
With each project, I get more confident in handling user input, applying logic, and organizing code.

Small tools like this Age Calculator are:

  • ๐Ÿ”ธ Easy to build
  • ๐Ÿ”ธ Great for beginners
  • ๐Ÿ”ธ Fun to share and showcase
  • ๐Ÿ”ธ Useful in real life, too!

๐Ÿช„ Final Thoughts

This wraps up Day 26 of my 90 Days Python Series.
If youโ€™re a beginner, I highly recommend starting your own daily project streak. Youโ€™ll be amazed at how much progress you make in just a few weeks. ๐Ÿ’ชโœจ

Iโ€™ll see you tomorrow with Day 27โ€™s project.
Hint: Itโ€™s going to be another practical and fun one! ๐Ÿš€


๐Ÿ‘‰ Follow my journey for more daily Python projects.

Top comments (0)