This program is supposed to take as in put dates in two formats: "month/day/year" and "Month day, Year". When I enter dates in the form "September 8, 2024" the program reprompts the user but when I enter "September 8 2024" (without the comma) it gives the required output. I tried to include in the program "elif ',' in input" but when I do none of the input gave the required output.
month_list = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
def parse_date(input):
try:
if "/" in input:
month, day, year = input.split("/")
month = int(month)
day=int(day)
else:
month, day, year = input.split()
if month in month_list and "," in day:
month = int(month_list.index(month)) + 1
day = int(day[-1])
else:
raise ValueError
if 1<=day<=31 and 1<=month<=12 and len(year)==4:
return year, month, day
else:
raise ValueError
except(ValueError, IndexError):
pass
def main():
while True:
input_date = input("Enter a date in the format month/day/year: ")
parse_date(input_date)
if parse_date(input_date):
year, month, day =parse_date(input_date)
month = int(month)
day = int(day)
print(f"{year}-{month:02}-{day:02}")
break
else: print("Invalid Format")
if __name__ == "__main__":
main()