The script works fine if I give it a date of 01/12/2021
. But if I give it another date like today's as 01/18/2021
or any other the script says that the date isn't valid and prompts the user to enter the date in again.
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)
This is the output I get if I put in some different dates:
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.
Why won't my script accept any date other than 01/12/2021
as valid input?
01/16/2021
is not a valid date, since the year has only 12 months and not 16;).
Your code should be like this:
def print_reports(interactive, aws_account, aws_account_number):
inputDate = input("Enter the date in format 'dd/mm/yyyy': ")
day, month, year = inputDate.split('/')
try:
datetime(int(day), int(month), int(year))
except ValueError as e:
print("Date is not valid.")
return False
print(f"Input date is valid: {inputDate}")
inputDate = datetime.strptime(inputDate,"%d/%m/%Y")
return True
myclient = connect_db()
mydb = myclient["aws_inventories"]
instance_col = mydb["ec2_list_" + inputDate]
while not success:
global success
success = print_reports(interactive, aws_account, aws_account_number)
You are asking for the format dd/mm/yyyy
. Yet, you are inputting (in what I assume) is a mm/dd/yyyy
format. If you try to parse 01/18/2021
in the format dd/mm/yyyy
, then you would get the result that it is the first day in the 18th month in the year 2021 (which, of course, is impossible - there are only 12 months in the year). To fix this, you are going to either have to change your format to mm/dd/yyyy
or input as 18/01/2021
.
To change the input checker to 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)
Note that it looks like you are using the function datetime
with the dd/mm/yyyy
format, but you are using the mm/dd/yyyy
format with datetime.strptime
in the original code. In this code, it only uses the mm/dd/yyyy
format, but you can always edit this easily to dd/mm/yyyy
.
Edit: Looks like you are using recursion. Of course, instead, you should be using a while
loop, as recursion is not needed here. Also, you are using the isValidDate
variable, but that is never actually used or needed:
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.