PWP Micro Project
PWP Micro Project
Micro Project On :-
“Age Calculator ”
Under The Guidance Of :-
Prof. Sawase A.A
For The Subject :-
Programing With Python (PWP)(22616)
Department Of
Computer Science And Engineering
In Academic Year
2024-25
Submitted By:- Devkar Krushna Laxman (2214920322)
1
CERTIFICATE
This is to certify that Devkar Krushna Laxman from Mitthulalji Sarada Polytechnic
Beed Institute Having Enrollment no. 2214920322 has completed Micro Project of third year
Programing With Python subject having Title Age Calculator during academic year 2024-2025
The project completed by Student under the guidance of the faculty Prof. Sawase A.A.
2
Index
2. Certificate 2
3. Introduction 3
4. Acknowledgement 4
5. Objective 6
6. Requirement 6
7. Code 9
8. Output 10
9. Conclusion 11
3
Introduction to the Age Calculator Python Project
The Age Calculator project is a Python-based application designed to calculate the exact age of
a person based on their birthdate. This project provides a practical example of how Python can
be used to manipulate and calculate date and time, making it an excellent starting point for
understanding date handling and user input validation.
In this project, the user is prompted to input their birthdate in the format YYYY-MM-DD. The
program then calculates the difference between the user's birthdate and the current date to
determine their age in terms of years, months, and days. The program also handles edge cases,
such as invalid date inputs or incorrect formats, ensuring that the application works reliably and
efficiently.
To create a functional Age Calculator using Python that can compute a person's age in
years, months, and days.
To understand how to handle and manipulate dates in Python using the built-in datetime
module.
To improve the user's experience by providing clear and formatted output and by
handling incorrect or invalid input.
Key Features:
1. User Input: The program asks the user to enter their birthdate in a specific format
(YYYY-MM-DD).
2. Date Calculations: The program calculates the difference between the current date and
the user's birthdate, accounting for the number of years, months, and days.
3. Error Handling: The application includes robust error handling, ensuring that users
cannot proceed with invalid inputs.
4. Readable Output: The user's age is displayed in a human-readable format that includes
years, months, and days.
Learning Outcomes:
Date and Time Manipulation: Using Python’s datetime module to perform operations
like calculating the difference between two dates.
User Input Validation: Handling user inputs, ensuring they follow the correct format
and managing incorrect or invalid inputs.
4
Basic Python Programming: Utilizing functions, loops, conditionals, and exception
handling to create a useful and efficient command-line application.
This project can be extended to incorporate more advanced features, such as:
Python Programming Language: The core functionality of this project is based on the
Python programming language, which has enabled the development of efficient and
simple solutions for age calculation using its built-in features and libraries.
datetime Module: The datetime module in Python has been pivotal in handling and
manipulating date and time. The project’s ability to calculate the difference between two
dates and adjust for months and days is powered by this module. The precise date
manipulation functionality of datetime made it easier to compute the age.
Text Editors and IDEs: The development of this project was carried out using [[IDE
name] or Text Editor], which facilitated coding with syntax highlighting, error detection,
and easy execution of Python code.
5
3. Acknowledgment for Guidance and Support:
Mentors and Instructors: I would like to express my sincere thanks to my mentors and
instructors for their continuous encouragement and guidance throughout the course of
this project. Their feedback helped refine the implementation and improve my coding
skills.
Peers and Colleagues: I extend my gratitude to my peers and colleagues who assisted in
reviewing the code, provided constructive feedback, and contributed their ideas to
enhance the functionality of the project.
This project has allowed me to further develop my skills in programming with Python,
particularly in date manipulation and user input validation. The process of building the Age
Calculator has greatly enhanced my understanding of Python’s built-in modules and how to
combine them to solve real-world problems. I acknowledge the effort and learning it took to
implement each aspect of the project, from error handling to formatting the output in a user-
friendly way.
The Age Calculator project calculates the user's age in years, months, and days based on their
inputted birthdate. The program takes the user's birthdate, calculates the difference between the
current date and the birthdate, and outputs the user's age in a readable format. This is a basic
Python project that helps in understanding date manipulation, user input validation, and working
with Python’s built-in libraries.
1. Objective:
To create a Python program that calculates a person’s age based on their birthdate input.
2. Requirements:
6
3. Libraries/Modules Used:
datetime: Python’s built-in module used to work with dates and times. It allows us to get the
current date and time, as well as perform date manipulations, such as finding the difference
between two dates.
First, the program asks the user to input their birthdate in the YYYY-MM-DD format. We use
input() to capture the date. Since users may provide the wrong format, we need to validate the
input.
python
Copy
def get_birthdate():
while True:
try:
# Ask the user to input their birthdate in YYYY-MM-DD format
birthdate_input = input("Enter your birthdate (YYYY-MM-DD): ")
birthdate = datetime.strptime(birthdate_input, "%Y-%m-%d")
return birthdate
except ValueError:
print("Invalid date format. Please use YYYY-MM-DD format.")
Explanation: We use the strptime() function from the datetime module to convert the
input string into a datetime object. If the user inputs an invalid date, the program catches the
error using ValueError and prompts the user to try again.
Once the birthdate is valid, we calculate the age by comparing it to the current date using the
datetime.today() method. We calculate the age in years, months, and days.
python
Copy
def calculate_age(birthdate):
today = datetime.today()
# Calculate the difference between the current date and the birthdate
age_in_years = today.year - birthdate.year
age_in_months = today.month - birthdate.month
age_in_days = today.day - birthdate.day
# Adjust the months and days if the current day or month hasn't been
reached yet
if age_in_days < 0:
7
age_in_months -= 1
age_in_days += (today.replace(month=today.month-1) if today.month !=
1 else today.replace(month=12)).day
if age_in_months < 0:
age_in_years -= 1
age_in_months += 12
Explanation:
o We calculate the year difference, month difference, and day difference.
o If the current day hasn't yet occurred in the current month, we subtract one month and
adjust the day difference by adding the number of days in the previous month.
o If the current month hasn't yet occurred, we subtract one year and adjust the month
difference by adding 12.
python
Copy
def display_age(age_in_years, age_in_months, age_in_days):
print(f"Your age is: {age_in_years} years, {age_in_months} months, and
{age_in_days} days.")
Step 4: Combine Everything in the Main Program
We then combine all the functions into a main program that calls each function in sequence.
python
Copy
def main():
birthdate = get_birthdate()
age_in_years, age_in_months, age_in_days = calculate_age(birthdate)
display_age(age_in_years, age_in_months, age_in_days)
if __name__ == "__main__":
main()
Explanation:
o The program starts by calling the get_birthdate() function to get the user’s input.
o The calculate_age() function computes the age.
o The display_age() function displays the result.
8
5. Full Code:
python
Copy
from datetime import datetime
# Calculate the difference between the current date and the birthdate
age_in_years = today.year - birthdate.year
age_in_months = today.month - birthdate.month
age_in_days = today.day - birthdate.day
if age_in_months < 0:
age_in_years -= 1
age_in_months += 12
if __name__ == "__main__":
main()
9
6. Sample Output:
bash
Copy
Enter your birthdate (YYYY-MM-DD): 1990-05-15
Your age is: 34 years, 8 months, and 5 days.
bash
Copy
Enter your birthdate (YYYY-MM-DD): 2000-01-01
Your age is: 25 years, 0 months, and 19 days.
7. Error Handling:
The program handles incorrect date formats. If the user enters an invalid date (e.g., 2022-13-45),
the program will prompt them again until a valid date is entered.
bash
Copy
Enter your birthdate (YYYY-MM-DD): 2022-13-45
Invalid date format. Please use YYYY-MM-DD format.
Enter your birthdate (YYYY-MM-DD): 1995-12-20
Your age is: 29 years, 1 months, and 1 days.
8. Enhancements to Consider:
1. Leap Year Handling: You could improve handling of leap years when the user is born on
February 29th.
2. Detailed Time Calculation: Modify the program to calculate and display age in terms of days,
hours, minutes, and seconds.
3. GUI Interface: You could use libraries like Tkinter to create a graphical user interface (GUI) for a
more user-friendly experience.
4. Age Validation: Add checks to make sure the user’s birthdate is not in the future.
5. Age in Total Days: You could add functionality to display the total number of days the user has
lived.
10
Conclusion:
This project is a great way to practice using Python’s built-in datetime module for handling
dates. It helps in understanding how to calculate date differences, manipulate user input, and
build a simple but functional application. With the given enhancements, you can further expand
this project into more advanced applications.
11