Skip to content

Instantly share code, notes, and snippets.

@storlak
Last active December 21, 2023 16:30
Show Gist options
  • Save storlak/dedf3efa6811e18acaba84fa041d93eb to your computer and use it in GitHub Desktop.
Save storlak/dedf3efa6811e18acaba84fa041d93eb to your computer and use it in GitHub Desktop.
Calculating your age via datetime library. Soon to be updated to include months, days.
from datetime import datetime
# what is my age?
while True:
try:
birth_year = int(input("Enter your birth year: "))
current_year = datetime.now().year
age = current_year - birth_year
#if birth_year <= 0 or birth_year < 1910 or birth_year >= current_year:
if not 1910 < birth_year < current_year:
print("Enter a valid birth year.")
else:
print(f"You're {age} years old.")
break
except ValueError:
print("Enter a valid year.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment