months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
while True:
user_input = input("Date: ")
parts = user_input.split("/")
if len(parts) == 3:
month, day, year = parts
try:
month = int(month)
day = int(day)
year = int(year)
if 1 <= month <= 12 and 1 <= day <= 31:
break
except ValueError:
pass
else:
parts = user_input.split()
if len(parts) == 3:
month_name, day, year = parts
try:
month = months.index(month_name) + 1
day = int(day.strip(","))
year = int(year)
if 1 <= month <= 12 and 1 <= day <= 31:
break
except (ValueError, IndexError):
pass
print("Invalid date format. Please try again.")
print(f"{year}-{month:02}-{day:02}")
Here’s the code. And the issue is when I input
September 8 1636
I should’ve got the invalid error but it’s reprompting, here’s the output:
Date: September 8 1636
1636-09-08
Need help to fix this.