I'm doing an event planner, and I wanted to prompt start date with time and end date with time. The validation works until the start date and start time, but problem comes up during the end date and time. When the end date format is incorrect, it won't prompt end date again, instead it prompt start time again and same happens to end time.
import datetime as dt
date_format = "%d-%m-%Y"
time_format = "%H:%M"
start_date = input("Please enter start date: ")
while True:
try:
dt.datetime.strptime(start_date, date_format)
start_time = input("Please enter start time: ")
try:
dt.datetime.strptime(start_time, time_format)
end_date = input("Please enter end date: ")
try:
dt.datetime.strptime(end_date, date_format)
end_time = input("Please enter end time: ")
try:
dt.datetime.strptime(end_time, time_format)
print(start_date,start_time)
print(end_date,end_time)
except:
print("This is the incorrect time string format. It should be HH:MM")
except:
print("This is the incorrect date string format. It should be DD-MM-YYYY")
except:
print("This is the incorrect time string format. It should be HH:MM")
except:
print("This is the incorrect time string format. It should be DD-MM-YYYY")
start_date = input("Please enter start date: ")