Age Calculator Using Python

How to Make Age Calculator Using Python 

Age Calculator Using Python

An age calculator is a simple program that takes a person’s  age and calculates their days & month based on the current age. Python is a popular programming language that is great for building such applications. In this article, we will be using Python’s built-in time module.

Hello coder’s welcome to the codewithrandom blog .In this blog post, we’ll walk you through how to create an age calculator using Python, step by step. Before we get started, make sure you have Python installed on your computer.

How to Make Age Calculator Using Python 

How to Build a Music Player using Python

How to Build a Alarm Clock using Python

#age calculator

To create age calculator  in Python , you can follow these steps:

Step 1: Import the time module  in Python, we’ll use the time module. Start by importing it at the beginning of your script:

import time
from calendar import isleap

Step 2: judge the leap year

def judge_leap_year(year):
    if isleap(year):
        return True
    else:
        return False

Step 3: returns the number of days in each month

def month_days(month, leap_year):
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    elif month in [4, 6, 9, 11]:
        return 30
    elif month == 2 and leap_year:
        return 29
    elif month == 2 and (not leap_year):
        return 28


name = input("input your name: ")
age = input("input your age: ")
localtime = time.localtime(time.time())

year = int(age)
month = year * 12 + localtime.tm_mon
day = 0

begin_year = int(localtime.tm_year) - year
end_year = begin_year + year

Step 4: calculate the days

for y in range(begin_year, end_year):
    if (judge_leap_year(y)):
        day = day + 366
    else:
        day = day + 365

leap_year = judge_leap_year(localtime.tm_year)
for m in range(1, localtime.tm_mon):
    day = day + month_days(m, leap_year)

day = day + localtime.tm_mday
print("%s's age is %d years or " % (name, year), end="")
print("%d months or %d days" % (month, day))


Age Calculator Using Python

complete code👇👇👇

# -*- coding: utf-8 -*-
import time
from calendar import isleap

# judge the leap year
def judge_leap_year(year):
    if isleap(year):
        return True
    else:
        return False


# returns the number of days in each month
def month_days(month, leap_year):
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    elif month in [4, 6, 9, 11]:
        return 30
    elif month == 2 and leap_year:
        return 29
    elif month == 2 and (not leap_year):
        return 28


name = input("input your name: ")
age = input("input your age: ")
localtime = time.localtime(time.time())

year = int(age)
month = year * 12 + localtime.tm_mon
day = 0

begin_year = int(localtime.tm_year) - year
end_year = begin_year + year

# calculate the days
for y in range(begin_year, end_year):
    if (judge_leap_year(y)):
        day = day + 366
    else:
        day = day + 365

leap_year = judge_leap_year(localtime.tm_year)
for m in range(1, localtime.tm_mon):
    day = day + month_days(m, leap_year)

day = day + localtime.tm_mday
print("%s's age is %d years or " % (name, year), end="")
print("%d months or %d days" % (month, day))

Output 👇👇

 Age Calculator

Conclusion

In this blog post, we’ve shown you how to create an age calculator using Python. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.



Leave a Reply