python check age code example
Example 1: age check python
import datetime
try:
current_year = datetime.datetime.now().year
print(current_year)
print('Must be over 18!')
age = int(input('How old are you? '))
if age > 18 or age == 18:
print('You are over 18!')
born_year = int(input('What year were you born in? '))
born_year_output = current_year - born_year
born_year_minus = born_year_output - 1
if born_year_output == age:
print('You are really over 18!')
name = input('What is your first name? ')
last_name = input('What is your last name? ')
print(f'You are {age} years old and your name is {name} {last_name}')
elif born_year_minus == age:
print('You are really over 18!')
name = input('What is your first name? ')
last_name = input('What is your last name? ')
print(f'You are {age} years old and your name is {name} {last_name}')
else:
print('You are not over 18!')
quit()
else:
print('you are not over 18!')
quit()
text_file = open("userdata.txt", "w")
n = text_file.write(f'Name: {name} {last_name}, age: {age}. born in {born_year} \\')
text_file.close()
except Exception as e:
print(f'Error: {e}')
quit()
Example 2: age check python
import datetime
import time
while True:
age = input('What year where you born? ')
current_time = datetime.datetime.now().year
if age.isdigit() is False:
print('Please enter a number')
quit()
elif len(age) != 4:
print('Please enter a valid year!')
quit()
elif age.isdigit() and len(age) == 4:
int_age = int(age)
if int_age < 1900:
print('Please enter a valid year!')
quit()
elif int_age > current_time:
print('Please enter a valid year!')
quit()
final_age = current_time - int_age
print('You are', final_age, 'or', final_age - 1, 'years old')
time.sleep(1)