Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么';我的脚本是否接受有效日期作为用户输入?_Python - Fatal编程技术网

Python 为什么';我的脚本是否接受有效日期作为用户输入?

Python 为什么';我的脚本是否接受有效日期作为用户输入?,python,Python,如果我给它一个日期01/12/2021,脚本运行良好。但如果我给它另一个日期,比如今天的as01/18/2021或任何其他日期,脚本会说该日期无效,并提示用户再次输入该日期 def print_reports(interactive,aws_account,aws_account_number): inputDate = input("Enter the date in format 'dd/mm/yyyy': ") day,month,year = inpu

如果我给它一个日期
01/12/2021
,脚本运行良好。但如果我给它另一个日期,比如今天的as
01/18/2021
或任何其他日期,脚本会说该日期无效,并提示用户再次输入该日期

def print_reports(interactive,aws_account,aws_account_number):
    inputDate = input("Enter the date in format 'dd/mm/yyyy': ")
    day,month,year = inputDate.split('/')
    isValidDate = True
    try:
        datetime(int(year),int(month),int(day))
    except ValueError :
        isValidDate = False
        print("Date is not valid.")
        print_reports(interactive,aws_account,aws_account_number)

    if(isValidDate) :
        print(f"Input date is valid: {inputDate}")
        format= "%m%d%Y"
        inputDate = datetime.strptime(inputDate,"%m/%d/%Y")
        inputDate = inputDate.strftime(format)
    else:
        print(f"Input date is not valid: {inputDate}")
        print_reports(interactive,aws_account,aws_account_number)
    myclient = connect_db()
    mydb = myclient["aws_inventories"]
    instance_col = "ec2_list_" + inputDate
    instance_col = mydb[instance_col]
    print_reports(interactive,aws_account,aws_account_number)
这是我输入不同日期后得到的输出:

Enter the date in format 'dd/mm/yyyy': 01/12/2021
Input date is valid: 01/12/2021
Enter the date in format 'dd/mm/yyyy': 01/13/2021
Date is not valid.
Enter the date in format 'dd/mm/yyyy': 01/14/2021
Date is not valid.
Enter the date in format 'dd/mm/yyyy': 01/15/2021
Date is not valid.
Enter the date in format 'dd/mm/yyyy': 01/16/2021
Date is not valid.

为什么我的脚本不接受除
01/12/2021
之外的任何日期作为有效输入?

01/16/2021
不是有效日期,因为一年只有12个月,而不是16个月;)
您的代码应该如下所示:

def print_报告(交互式、aws_账户、aws_账户编号):
inputDate=输入(“输入格式为'dd/mm/yyyy':'的日期”)
日、月、年=inputDate.split(“/”)
尝试:
日期时间(日、月、年)
除ValueError为e外:
打印(“日期无效”)
返回错误
打印(f“输入日期有效:{inputDate}”)
inputDate=datetime.strTime(inputDate,“%d/%m/%Y”)
返回真值
myclient=connect_db()
mydb=myclient[“aws\U库存”]
实例列=mydb[“ec2列表”+输入日期]
虽然没有成功:
全球成功
成功=打印报告(交互式、aws\U帐户、aws\U帐户编号)

您要求的格式是
dd/mm/yyyy
。然而,您输入的(我假设)是
mm/dd/yyyy
格式。如果您尝试以
dd/mm/yyyy
格式解析
01/18/2021
,那么您将得到的结果是2021年第18个月的第一天(当然,这是不可能的,一年中只有12个月)。要解决此问题,您必须将格式更改为
mm/dd/yyyy
,或将其输入为
18/01/2021

要将输入检查器更改为
mm/dd/yyyy

def print_reports(interactive, aws_account, aws_account_number):
    inputDate = input("Enter the date in format 'mm/dd/yyyy': ")
    month, day, year = inputDate.split('/') # changed order of month and day
    isValidDate = True
    try:
        datetime(int(year), int(month), int(day))
    except ValueError:
        isValidDate = False
        print("Date is not valid.")
        print_reports(interactive, aws_account, aws_account_number)

    if isValidDate:
        print(f"Input date is valid: {inputDate}")
        format = "%m%d%Y"
        inputDate = datetime.strptime(inputDate,"%m/%d/%Y")
        inputDate = inputDate.strftime(format)
    else:
        print(f"Input date is not valid: {inputDate}")
        print_reports(interactive,aws_account,aws_account_number)
    myclient = connect_db()
    mydb = myclient["aws_inventories"]
    instance_col = "ec2_list_" + inputDate
    instance_col = mydb[instance_col]
    print_reports(interactive,aws_account,aws_account_number)
请注意,看起来您使用的是带有
dd/mm/yyyy
格式的函数
datetime
,但在原始代码中使用的是带有
datetime.strtime
格式的
mm/dd/yyyyy
。在这段代码中,它只使用
mm/dd/yyyy
格式,但您始终可以轻松地将其编辑为
dd/mm/yyyy

编辑:看起来您正在使用递归。当然,您应该使用
while
循环,因为这里不需要递归。此外,您正在使用
isValidDate
变量,但实际上从未使用或需要:

def print_reports(interactive, aws_account, aws_account_number):
    while True:
        inputDate = input("Enter the date in format 'mm/dd/yyyy': ")
        month, day, year = inputDate.split('/') # changed order of month and day
        try:
            datetime(int(year), int(month), int(day))
        except ValueError:
            print("Date is not valid.")
            continue

        print(f"Input date is valid: {inputDate}")
        format = "%m%d%Y"
        inputDate = datetime.strptime(inputDate,"%m/%d/%Y")
        inputDate = inputDate.strftime(format)

        myclient = connect_db()
        mydb = myclient["aws_inventories"]
        instance_col = "ec2_list_" + inputDate
        instance_col = mydb[instance_col]

一年中只有12个月,尽管这似乎不完全是您的问题,但您可能希望更新输出以不显示dd/mm/yyyy,而不在函数内部调用函数重新开始。使用while循环。您所做的是所谓的递归,在这里不合适。一年中只有12个月,您将dd/mm/yyyy格式与mm/dd/yyyy格式混淆。我投票决定结束此操作,因为键入错误/不可复制
day,month,year=inputDate.split('/')
对于
'01/13/2021'
的输入,应该是
month,day,year=inputDate.split('/')
。如果打印异常中的错误,则表明
日期无效。月份必须在1..12中
。仅供参考:由打字错误引起的问题将被关闭,并被删除。我认为同样的事情,看起来他正在以一种方式打印,而以另一种方式格式化,递归地调用打印报告是不正确的