This article will explore how to build a simple age calculator using Python.

Table of Contents



Introduction

In this tutorial we will build a simple program to calculate user’s age using Python.

It’s a perfect tutorial for beginners starting to learn Python and those who want to create one of their first small projects.

In this tutorial we will work with the datetime module that supplies classes for manipulating dates and times. It is already built-in in Python, so there is nothing additional to install.


Build age calculator in Python

Let’s start with importing the required dependency:


#Import the required dependency
from datetime import date

Then we will ask the user to input their birthday, and then we will create a datetime object with their birthday:


#Ask user to input year, month, and day of their birthday
#and output a datetime object of their birthday
def get_user_birthday():
    birth_year = int(input('Please enter your birth year: '))
    birth_month = int(input('Please enter your birth month: '))
    birth_day = int(input('Please enter your birth day: '))

    #Convert user input into datetime object
    user_birthday = date(birth_year, birth_month, birth_day)

    return user_birthday

Now we can calculate the user’s age:


#Define function to calculate user's age
def calculate_age(user_birthday):
    #Get current date
    today = date.today()
    #Calculate the years difference
    year_diff = today.year - user_birthday.year
    #Check if birth month and birth day precede current month and current day
    precedes_flag = ((today.month, today.day) < (user_birthday.month, user_birthday.day))
    #Calculate the user's age
    age = year_diff - precedes_flag
    #Return the age value
    return age

And now we can execute the code:


if __name__ == "__main__":
    current_age = calculate_age(user_birthday)
    print(f"Your age is: {current_age}")

Testing the code with a sample birthday:

Please enter your birth year: 1999
Please enter your birth month: 02
Please enter your birth day: 01

And you should get:

Your age is: 23

Complete code


#Import the required dependency
from datetime import date

#Ask user to input year, month, and day of their birthday
#and output a datetime object of their birthday
def get_user_birthday():
    birth_year = int(input('Please enter your birth year: '))
    birth_month = int(input('Please enter your birth month: '))
    birth_day = int(input('Please enter your birth day: '))

    #Convert user input into datetime object
    user_birthday = date(birth_year, birth_month, birth_day)

    return user_birthday

#Define function to calculate user's age
def calculate_age(user_birthday):
    #Get current date
    today = date.today()
    #Calculate the years difference
    year_diff = today.year - user_birthday.year
    #Check if birth month and birth day preced current month and current day
    precedes_flag = ((today.month, today.day) < (user_birthday.month, user_birthday.day))
    #Calculate the user's age
    age = year_diff - precedes_flag
    #Return the age value
    return age


if __name__ == "__main__":
    user_birthday = get_user_birthday()
    current_age = calculate_age(user_birthday)
    print(f"Your age is: {current_age}")

Conclusion

In this article we covered how you can create an age calculator in Python. I also encourage you to check out my other posts on Python Programming.

Feel free to leave comments below if you have any questions or have suggestions for some edits.