I was requested to create an legal age verification program using a user's year of birth. I have managed to limit the users integer input to 4 digits but it does allow the user to enter any number within a four digit range (0, 234, 1234 etc). How to I force a 4 digit input and forbid 'inputs' below 1940?
I have tried a WHILE statement, IF statement, a len() function but none of them work.
# Welcome user to the program
greeting = "Hello! Welcome to the age calculator."
print(greeting)
print("\n")
# Input current year
current_year = 2019
# Request the users year of birth and provide eg of birth year to guide user input to 4 digits
# Set integer digit limit to max 4
birth_year = int(input("Please confirm the year of your birth " + "\n" + "eg:'1989'" + "\n")[:4])
# Use basic sum to calculate users age using this year
age = current_year - birth_year
print(age)
# use IF statement to add legal age
if age >= 18:
print("Congrats you are old enough")
Expected outcome is a valid year of birth between 1940 and the current year.