Creating a program that prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636

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.

I’m not sure if I’m seeing what is wrong in the example. Could you elaborate? Why this case should end up with error?

I think the issue is the input for the date could either be:

September 8, 1636

or

9/8/1636

The date the user entered was missing the comma, so apparently that is not a valid input date format.

@ritamgit-alt In your else code block, you will not be able to just split on whitespace like split() does. You will have to be able to validate the comma appears and then either remove it before splitting or split using comma as the delimeter to get a list containing the text month as one item and the day and year as the second item. Then, you would split the second item using whitespace as delimeter to get a list contain day and years as items.

:( input of September 8 1636 results in reprompt
    expected program to reject input, but it did not

that’s the error I got, so if I input September 8 1636 as my code I should’ve got the invalid output but instead of that I keep getting reprompt.

:( input of September 8 1636 results in reprompt
    expected program to reject input, but it did not

that’s the error I got, so if I input September 8 1636 as my code I should’ve got the invalid output but instead of that I keep getting reprompt.

As my code it should remove the comma from day variable but if I input

September 8 1636

so there’s no comma in input so I should get invalid output but it’s happening totally opposite .

You stated in the beginning that for a text date to be considered valid input, it should have a comma separating the month name and the rest of the date (day and year). Is that still correct? If the answer is “Yes”, then you need to check to make sure the comma exists before splitting the date. If the comma is missing, then you should reject as invalid date.