I need to use try-except to validate the input string of a date and time.The input format should be MM/DD/YYYY HH:MM:SS. If the validate function returns false, then it's not valid.I tried a invalid input, but the try-except didn't work as expected. The error message wasn't printed. What's wrong with my code? How to fix it? Thanks.
def validate_user_input(input_string):
# check the input validation
validation = True
if input_string[2] != "/" or input_string[5] != "/":
validation = False
elif int(date) > 31 or int(date) < 1:
validation = False
elif int(month) > 12 or int(month) < 1:
validation = False
elif int(hour) < 0 or int(hour) > 24:
validation = False
elif int(minute) < 0 or int(minute) > 59:
validation = False
elif int(second) < 0 or int(second) > 59:
validation = False
return validation
input = input("Please input a date and time within the format of MM/DD/YYYY HH:MM:SS")
# get the information from the input
year = input[6:10]
month = input[0:2]
date = input[3:5]
hour = input[11:13]
minute = input[14:16]
second = input[17:19]
# try-except
try:
validate_user_input(input) == False
print("Your input is valid!")
except:
print("Error: the input date and time is invalid!")
The except
will only catch an exception if one is thrown and not caught by an earlier try-except
. You are not raise
ing anything in your code, so the except
block will never run. You should instead throw an error, perhaps a ValueError
, when the validate_user_input
function returns False
:
try:
if not validate_user_input(input):
raise ValueError("Bad input")
except:
...
Use an if statement instead. This should be simpler than using try and except.
if validate_user_input(input) == False:
print("Your input is valid!")
else:
print("Error: the input date and time is invalid!")
# try-except
try:
validate_user_input(input) == False
print("Your input is valid!")
except:
print("Error: the input date and time is invalid!")
Seems like you're going to validate the entered date & time from the input. But this code lines doesn't do anything. It check the condition without doing anything, @dogman288 answer make it happen. I would like to suggest a library call python-dateutil . Within few lines you can do your validations very easily, I hope this will save your time. Just comment the relevant outputs.
from dateutil.parser import parse
from dateutil.parser._parser import ParserError
def validate_user_input(input_string: str):
try:
parse(input_string)
print("Your input is valid!")
except ParserError as error:
print(error)
validate_user_input("12/31/2005 23:59:6") # Your input is valid!
validate_user_input("13/31/2005 23:59:6") # month must be in 1..12: 13/31/2005 23:59:6
validate_user_input("12/32/2005 23:59:6") # day is out of range for month: 12/32/2005 23:59:6
validate_user_input("12/31/200511111 23:59:6") # year 200511111 is out of range: 12/31/200511111 23:59:6
validate_user_input("12/31/2005 24:59:6") # hour must be in 0..23: 12/31/2005 24:59:6
validate_user_input("12/31/2005 23:61:6") # minute must be in 0..59: 12/31/2005 23:61:6
validate_user_input("12/31/2005 23:59:61") # second must be in 0..59: 12/31/2005 23:59:61
validate_user_input("") # String does not contain a date:
validate_user_input("some_string") # Unknown string format: some_string
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.