How to Find your Age Base on Birthdate in Python

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.

Getting Started:

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/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. from datetime import datetime
  2.  
  3. ret = False
  4.  
  5. def calculate_age(birth_date):
  6. today = datetime.today()
  7. age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
  8. return age
  9.  
  10.  
  11. while True:
  12.  
  13.  
  14. print("\n====================== Find your Age Base on Birthdate ====================== \n\n")
  15.  
  16.  
  17. birth_date_str = input("Enter your birthdate in YYYY-MM-DD format: ")
  18. birth_date = datetime.strptime(birth_date_str, "%Y-%m-%d")
  19. age = calculate_age(birth_date)
  20. print("Your age is:", age)
  21.  
  22.  
  23. opt = input("Do you want to try again?(yes/no): ")
  24.  
  25. if opt.lower() == 'yes':
  26. ret=False
  27. elif opt.lower() == 'no':
  28. ret=True
  29. print("Exiting program....")
  30.  
  31. else:
  32. print("Please enter yes/no:")
  33. break
  34.  
  35. if ret == False:
  36. 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.

Output:

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

Python Tutorials

Add new comment