1

I want to be able to check the year entered by user is valid:

  • Only a positive integer
  • Between the range 1998-current year

If no year is mentioned, then use the current year

Is there a more efficient way to do this?

from datetime import datetime

current_year = datetime.now().year

input_year = int(input("Enter Year Here >>"))

if input_year is None:
    input_year = current_year
elif (1998 > input_year) or (input_year > current_year):
    print("Please specify a date between 1998 to {}".format(current_year))
else:
AlyssaAlex
  • 696
  • 2
  • 9
  • 33

1 Answers1

3

If the entered year is empty int(input("Enter Year Here >>")) will fail since you are trying to convert an empty string, so you need to take input in as a string, and check if string is None, if it is, then assign current_year to input year, otherwise convert input string to int

Other optimiziation you can do is chained comparison

from datetime import datetime

current_year = datetime.now().year
#Take input as string
input_year = input("Enter Year Here >>")

#If input is None, use current year, else convert input to int
if not input_year:
    input_year = current_year
else:
    input_year = int(input_year)

#Use chained comparison
if not 1998 < input_year < current_year:
    print("Please specify a date between 1998 to {}".format(current_year))
else:
    print("You are good")

Possible outputs are

Enter Year Here >>
Please specify a date between 1998 to 2019

Enter Year Here >>1990
Please specify a date between 1998 to 2019

Enter Year Here >>2020
Please specify a date between 1998 to 2019

Enter Year Here >>-1000
Please specify a date between 1998 to 2019

Enter Year Here >>2010
You are good

To keep checking for input, just wrap the code in a while true loop

while True:

    current_year = datetime.now().year
    #Take input as string
    input_year = input("Enter Year Here >>")

    #If input is None, use current year, else convert input to int
    if not input_year:
        input_year = current_year
    else:
        input_year = int(input_year)

    #Use chained comparison
    if not 1998 < input_year < current_year:
        print("Please specify a date between 1998 to {}".format(current_year))
    else:
        print("You are good")

Furthermore, you are use a string like quit to break your loop, and perhaps do your empty string check and input year assignment in a single line as well!

from datetime import datetime

while True:

    current_year = datetime.now().year
    input_year = input("Enter Year Here! Type quit to stop >>")
    if input_year.lower() == 'quit':
        break
    input_year = int(input_year) if input_year else current_year

    if 1998 < input_year < current_year:
        print('You are good')
    else:
        print("Please specify a date between 1998 to {}".format(current_year))

If you want to check if user inputs a string or a float, you can do try/except and rely on ValueError to check if the string can be cast to an int or not! If string can be cast as int, it won't throw a ValueError, otherwise it will

def check_int(s):

    is_int = False
    try:
        int(s)
        is_int = True
    except ValueError:
        pass

    return is_int

print(check_int('a'))
print(check_int('4.0'))
print(check_int(5))

The output will be

False
False
True

Finally, combining all of them we get

from datetime import datetime

def check_int(s):

    is_int = False
    try:
        int(s)
        is_int = True
    except ValueError:
        pass

    return is_int


while True:

    current_year = datetime.now().year
    input_year = input("Enter Year Here! Type quit to stop >>")
    if input_year.lower() == 'quit':
        break
    if not input_year:
        input_year = current_year
    elif check_int(input_year):
        input_year = int(input_year)
    else:
        print('Provide a number as year')
        continue

    if 1998 < input_year < current_year:
        print('You are good')
    else:
        print("Please specify a date between 1998 to {}".format(current_year))

The outputs will be

Enter Year Here! Type quit to stop >>
Please specify a date between 1998 to 2019
Enter Year Here! Type quit to stop >>hello
Provide a number as year
Enter Year Here! Type quit to stop >>-1234
Please specify a date between 1998 to 2019
Enter Year Here! Type quit to stop >>2000
You are good
Enter Year Here! Type quit to stop >>quit
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40