Last active
December 21, 2023 16:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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