Age Calculator
Calculate age from birth date.
IntermediatePhase 6 - Category 5: Real-World Logic ProblemsExample 8 of 15
age-calculator.py
1from datetime import date23# Take birth date4year = int(input("Enter birth year: "))5month = int(input("Enter birth month: "))6day = int(input("Enter birth day: "))78# Calculate age9today = date.today()10birth_date = date(year, month, day)11age = today.year - birth_date.year1213# Adjust if birthday hasn't occurred this year14if (today.month, today.day) < (birth_date.month, birth_date.day):15 age -= 11617print(f"Age: {age} years")
Output
Enter birth year: 2000 Enter birth month: 5 Enter birth day: 15 Age: 24 years
What's going on
Calculate age from birth date.
Key Concepts:
Get current date
Calculate year difference
Adjust if birthday not passed
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum