0
import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def get_month(month_ent):

    if month_ent in range(1,10):
        teto = f"{month_ent:02}"
        return teto
    else:
        return month_ent

def get_day(day_ent):

    if day_ent in range(1,10):
        teto = f"{day_ent:02}"
        return teto
    else:
        return day_ent

def main():
    while True:
        try:

            test = input("Date: ")

            input_date = re.split(", |/", test)

            input_month = input_date[0]
            input_day= int(input_date[1])
            #input_year = input_date[2]
            length_input = len(input_date)
            month_ent = (input_month.split(" "))[0]
            day_ent = int((input_month.split(" "))[1])

            if ((length_input == 2)  and (input_day.isnumeric()) and (month_ent in months) and (day_ent in range(1,32))):
                print(f"{input_day}-{(get_month((months.index(month_ent)) + 1))}-{get_day(day_ent)}")

            elif ((length_input == 3) and (int(input_month) in range(1,13)) and (input_day in range(1,32)) and (int(input_date[2]).isnumeric())):
                print(f"{input_date[2]}-{get_month(input_month)}-{get_day(input_day)}")

        except (ValueError, IndexError):
            pass



if __name__ == "__main__":
    main()

2 Answers 2

0

i changed the first main() function and i could output for this format eg. September 3, 2015 but for this format 12/12/1985, it allways prompt to enter the date again.

def main():
    while True:
        test = input("Date: ")
        input_date = re.split(", |/", test)
        try:

            input_month = input_date[0]
            input_day= int(input_date[1])
            #input_year = input_date[2]
            length_input = len(input_date)
            month_ent = (input_month.split(" "))[0]
            day_ent = int((input_month.split(" "))[1])

            if ((length_input == 2) and (month_ent in months) and (day_ent in range(1,32))):
                print(f"{input_day}-{(get_month((months.index(month_ent)) + 1))}-{get_day(day_ent)}")
                break

            else:
                if ((length_input == 3) and ((int(input_month)) in range(1,13)) and (input_day in range(1,32)) and ((int(input_date[2])))):
                    print(f"{int(input_date[2])}-{get_month (int(input_month))}-{get_day(input_day)}")
                    break

        except (ValueError, IndexError):
            pass
4
  • dont reply as an answer.. post this as a seperate question. mark the answer that worked as such if it solved the initial issue. this is not a back and forth dialogue. also please format your code properly
    – UpAndAdam
    Dec 1, 2023 at 17:26
  • instead of inserting a break into the if and else twice... just put a break after the if/else if statement and combine the else; if into a elif.... and this is on top of your code not parsing the / properly.. i would try a simple re.split test to see what you are doing wrong that is less involved.
    – UpAndAdam
    Dec 1, 2023 at 17:31
  • thank you for the answer. It work now
    – Horace
    Dec 2, 2023 at 15:05
  • if thats the case please go mark my answer as accepted and strongly consider upvoting it as well.
    – UpAndAdam
    Dec 2, 2023 at 19:24
0

you have nothing to get you out of your outermost while True loop. you need to add a break statement somewhere when the success case is reached.

also instead of inserting a break into the if and else twice... just put a break after the if/else if statement and combine the else; if into a elif.... and this is on top of your code not parsing the / properly.. i would try a simple re.split test to see what you are doing wrong that is less involved

2
  • i changed the first main() function and i could output for this format eg. "September 3, 2015" but for this format "12/12/1985", it allways prompt to enter the date again. I post the new code below.
    – Horace
    Nov 30, 2023 at 18:25
  • dont paste the new code below into an answer this is not a thread based forum its a question and answer site.
    – UpAndAdam
    Dec 1, 2023 at 17:27

You must log in to answer this question.

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