In this tutorial, we will program 'How to Find Your Age Based on Birthdate in Python'. We will learn how to find our actual age based on the given birthdate. The main goal here is to create a program that will easily calculate your age. I will provide a sample program to demonstrate the actual coding of this tutorial.
This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for finding your age. I will do my best to provide you with the easiest method of finding the actual age. So, let's start with the coding.
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
from datetime import datetime ret = False def calculate_age(birth_date): today = datetime.today() age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) return age while True: print("\n====================== Find your Age Base on Birthdate ====================== \n\n") birth_date_str = input("Enter your birthdate in YYYY-MM-DD format: ") birth_date = datetime.strptime(birth_date_str, "%Y-%m-%d") age = calculate_age(birth_date) print("Your age is:", age) opt = input("Do you want to try again?(yes/no): ") if opt.lower() == 'yes': ret=False elif opt.lower() == 'no': ret=True print("Exiting program....") else: print("Please enter yes/no:") break if ret == False: continue
This Python script calculates the age of a person based on their birthdate input in the format YYYY-MM-DD. It then prompts the user if they want to try again and continues looping until the user chooses to exit.
The How to Find your Age Base on Birthdate in Python source code that I provide can be download below. Please kindly click the download button.
There you have it we successfully created How to Find your Age Base on Birthdate in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language