Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c84e81c

Browse files
Create Age_Calculator.py
1 parent f05226e commit c84e81c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎Age_Calculator/Age_Calculator.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# importing the date module from the datetime package
2+
from datetime import date
3+
4+
# defining a function calculate the age
5+
def calculate_age(birthday):
6+
7+
# using the today() to retrieve today's date and stored it in a variable
8+
today = date.today()
9+
10+
# a bool representing if today's day, or month precedes the birth day, or month
11+
day_check = ((today.month, today.day) < (birthday.month, birthday.day))
12+
13+
# calculating the difference between the current year and birth year
14+
year_diff = today.year - birthday.year
15+
16+
# The difference in years is not enough.
17+
# We must subtract 0 or 1 based on if today precedes the
18+
# birthday's month/day from the year difference to get it correct.
19+
# we will subtract the 'day_check' boolean from 'year_diff'.
20+
# The boolean value will be converted from True to 1 and False to 0 under the hood.
21+
age_in_years = year_diff - day_check
22+
23+
# calculating the remaining months
24+
remaining_months = abs(today.month - birthday.month)
25+
26+
# calculating the remaining days
27+
remaining_days = abs(today.day - birthday.day)
28+
29+
# printing the age for the users
30+
print("Age:", age_in_years, "Years", remaining_months, "Months and", remaining_days, "days")
31+
32+
# main function
33+
if __name__ == "__main__":
34+
35+
# printing an opening statement
36+
print("Simple Age Calculator")
37+
38+
# asking user to enter birth year, birth month, and birth date
39+
birthYear = int(input("Enter the birth year: "))
40+
birthMonth = int(input("Enter the birth month: "))
41+
birthDay = int(input("Enter the birth day: "))
42+
43+
# converting integer values to date format using the date() method
44+
dateOfBirth = date(birthYear, birthMonth, birthDay)
45+
46+
# calling the function to calculate the age of a person
47+
calculate_age(dateOfBirth)

‎Age_Calculator/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Age_Calculator.py
2+
3+
Step1:- First of all, we need to import two libraries into our code. The first one is the time and date
4+
5+
Step2:- Now, just use Age_Calculator.py code run it on online r any complier
6+
7+
Step3:- It will frist your date of birth year!
8+
9+
Step4:- Then it will ask your birth month!
10+
11+
Step5:- Then it will ask your birth day!
12+
13+
Step6:- Finally it will just a date of you enterted and current date and time it will print the the years and months days!
14+
15+
conclusion:- It is useful to Calculate birthdays in years!

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /