0

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()

2 Answers 2

1

You have many problems in your code.

  • You never return anything from parse_date so formatted_date will always be False. You seem to possibly be relying on an implicit return of True but this is an extremely bad practice. If you want to return True, then return True.
  • You are printing in parse_date which is fine but you need to return what your main function is expecting too!
  • If a / is present why is there a conditional check before you assign the value for year? What is your if accomplishing? You need probably need return something here.
  • If the date doesn't have a / you have issues, you said 'alphanumberic' but that's not entirely correct there is a ',' and the order doesnt seem to match what you capture. Consider the example input case, the input is "September 8, 2024", your else statement says day, month, year = input.split() so day will equal 'September', month will equal '8,' and year will equal '2024'. You should be able to see how this is going to lead you to problems with the remainder of your logic.
  • In parse_date there is no reason for you to raise errors/exceptions, especially an index_error just return false, or raise a value_error.
    • Raising an error only makes sense if you are going to catch it ( yes do do this ) and log something sensible with it ( like tell the user what they did wrong or what unexpectedly failed) and intend to recover from it, or not catch it and want to give you a traceback to the problem.
0

I was able to fix the bugs. The problem relied on "int(day[-1])" I replaced it with "day.rstrip(',')".

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 = day.rstrip(",")
                 day = int(day)
            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")

main()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .