Help needed for below program

months=["January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
while True:
    date = input("Date:").strip()
    if "/" in date:
     month, day, year = date.split("/")
    elif "," in date:
        date = date.replace(",", "")
        month, day, year = date.split(" ")
    else:
        continue
    if month in months:
        month = months.index(month) +1
    try:
        if int(month) > 12 or int(day) > 31:
            continue
        else:
            break
    except ValueError:
        continue

print(year + "-" + f"{int(month):02}"+ "-" + f"{int(day):02}")

Hi Here is my code, Idont know How to rectify the below error.

Please help: Error message: Input of October/9/1701 result in reprompt expected program to reject input but it did not. Help please

Hello there,

We recommend familiarising yourself with how to ask a question on this forum. Specifically, you are more likely to get useful help, if you provide a helpful title and description:

1 Like

Thank you, i will follow that sure.

1 Like

What are all the valid inputs for dates to your program? I can see your program will accept a date like 10/17/2022 and print 2022-10-17.

1 Like

implement 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 , wherein the month in the latter might be any of the values in the list below:
Then output that same date in YYYY-MM-DD format. If the user’s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has
whether a month has 28, 29, 30, or 31 days

When I enter: October/9/1701, the output is 1701-10-09. What are you expecting the output to be instead?

1 Like

if we enter October/9/1701 it should reject input and print Date:

What do you mean by reject input? If you want to reject that input, then why would you still want to print the date?

1 Like

program should accept only below formats, yyy-mm-dd
if i enter 9/8/1636 or September 8, 1636, If i gave this format october/9/1701 it should print meg which i have inputted first, here I wrote this program handling the extension.

I don’t know what meg is.

meg = Input("Date: ")
If i gave the october/9/1701 its should prompt my input

Currently, if the input has / in it, you are splitting it into month, day, year. If you want month to be a number, then you should check to make sure it is or else continue which will cause code ask for input again.

1 Like

i gave else continue but it didn’t worked. if i gave small letter, Like :may/1/1701
it worked correctly, But if i gave May/1/1701 it didn’t worked out.

Please post your latest code.

months=["January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
while True:
    date = input("Date: ")
    if "/" in date:
        month, day, year = date.split("/")
    elif "," in date:
        date = date.replace(",", "")
        month, day, year = date.split(" ")
    else:
       continue
    if month in months:
        month = months.index(month) +1
    try:
        if int(month) > 12 or int(day) > 31:
            continue
        else:
            break
    except ValueError:
        continue

print(year + "-" + f"{int(month):02}"+ "-" + f"{int(day):02}")

You only added an else statement. Read my previous replies. I told you what logic needs to be incorporated to accomplish the task. The logic will take place inside the code block below the elif statement.