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

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

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

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.

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

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.

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}")

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.