Programming

Python Age Calculator: Calculate Your Age with This Simple Code

Discover your age effortlessly with this Python program! Learn step-by-step how to calculate your age using a simple Python script. Follow the easy guide and find out how to determine your age based on your birth year

Start!

#Python program to calculate age

def calculate_age(birth_year):
current_year = 2024 # replace with the current year
age = current_year - birth_year
return age

# Get user input for birth year
birth_year = int(input("Enter your birth year: "))

# Calculate and display the age
age = calculate_age(birth_year)
print(f"Your age is: {age} years")

Here is the Code!

from datetime import date

# Function to calculate age
def calculate_age(birthdate):
    today = date.today()
    age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
    return age

# Input birthdate
year = int(input("Enter your birth year (YYYY): "))
month = int(input("Enter your birth month (MM): "))
day = int(input("Enter your birth day (DD): "))

# Create a date object for the birthdate
birthdate = date(year, month, day)

# Calculate and print the age
print(f"You are {calculate_age(birthdate)} years old.")

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.