๐ 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! ๐งก
`
โ๏ธ How It Works โ Step by Step
-
Import the datetime module
- Pythonโs
datetime
makes it super easy to work with dates.
- Pythonโs
-
Ask for user input in the format
YYYY-MM-DD
. -
Convert the string into a date object using
datetime.strptime
. - Compare the entered date with the current date to find the difference.
- Extract years and months from the difference.
- Use a
while
loop so the program keeps running until the user says โno.โ - 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
๐ง 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)