如何检查用户输入的年份是否有效?

时间:2019-05-05 13:10:23

标签: python

我希望能够检查用户输入的年份是否有效:

  • 仅一个正整数
  • 在1998年至今的年份之间

如果未提及年份,则使用当前年份

有没有更有效的方法?

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:

1 个答案:

答案 0 :(得分:3)

如果输入的年份为空,int(input("Enter Year Here >>"))将由于尝试转换为空字符串而失败,因此您需要将输入作为字符串输入,并检查字符串是否为None(如果为None),然后分配current_year为输入年份,否则将输入字符串转换为int

您可以进行的其他优化是链式比较

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")

可能的输出是

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

要继续检查输入,只需将代码包装在while true循环中

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")

此外,您正在使用quit之类的字符串来中断循环,也许还可以在一行中进行空字符串检查和输入年份分配!

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))

如果要检查用户输入的是字符串还是浮点数,可以执行try / except并依靠ValueError来检查字符串是否可以转换为int形式!如果string可以强制转换为int,则不会抛出ValueError,否则会

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))

输出将为

False
False
True

最后,我们将所有这些结合起来

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))

输出将是

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