0

My program is expected to take date input such as September 4, 1995 or 9/4/1995 from the user and convert it to 1995-09-04. However, it is returning 1995-04-09. What I have done is to create a regex in the calendar_regex() function and match it with a string from the get_date() function to produce the output. Here's a snippet of my code:

import re, datetime

#Define the main function
def main():
    user_input = input("Enter a date: ")
    print(calendar_regex(get_date(user_input)))



#Define a function to match the output of the get_date() function to a regex pattern
def calendar_regex(text):
    while True:
        try:
            mo = re.search(r'\d{1}/\d{1}/\d{4}', text) #Create a pattern using regex
            date = datetime.datetime.strptime(text, '%d/%m/%Y').date()
            return date
        except ValueError:
            break



#Create a function to convert user input to a string that will be used as a search string in the Calendar_regex() function
def get_date(x):
    month_dict = {
    "January": "1",
    "February": "2",
    "March": "3",
    "April": "4",
    "May": "5",
    "June": "6",
    "July": "7",
    "August": "8",
    "September": "9",
    "October": "10",
    "November": "11",
    "December": "12"
    }

    while True:
        try:
            a, b, c = x.split(" ")
            d = month_dict.get(a) #Get the value pair of the key (month) typed by user
            e = d + " " + b + " " + c #Concatenate the string variables to form one string
            f = e.replace(",", "").replace(" ", "/") #Remove the comma xter and replace the white spaces with backslashes
            return f
        except ValueError:
            return x



#Call the main function
main()

Here's what I'm getting when I use check50:

:) outdated.py exists
:( input of 9/8/1636 outputs 1636-09-08
    expected "1636-09-08", not "1636-08-09\n"
:( input of September 8, 1636 outputs 1636-09-08
    expected "1636-09-08", not "1636-08-09\n"
:( input of 10/9/1701 outputs 1701-10-09
    expected "1701-10-09", not "1701-09-10\n"
:( input of October 9, 1701 outputs 1701-10-09
    expected "1701-10-09", not "1701-09-10\n"
:( input of " 9/8/1636 " outputs 1636-09-08
    expected "1636-09-08", not "Traceback (mos..."
:( input of 23/6/1912 results in reprompt
    expected program to reject input, but it did not
:( input of 10 December, 1815 results in reprompt
    expected program to reject input, but it did not
:( input of October/9/1701 results in reprompt
    expected program to reject input, but it did not
:( input of 1/50/2000 results in reprompt
    expected program to reject input, but it did not
:( input of December 80, 1980 results in reprompt
    expected program to reject input, but it did not
:( input of September 8 1636 results in reprompt
    expected program to reject input, but it did not

2 Answers 2

1

From the python doc

[strptime usage] Parse a string into a datetime object given a corresponding format

text is sent to the function with m/d/Y format

That correction should fix the "order" problem. There are other problems with input validation.

1
  • That really makes sense. I'll have to figure out how to work on the input validation. I'll also appreciate any ideas that you may have. Thank you so much!
    – Arctic Fox
    Commented Jan 11, 2023 at 2:28
0

One thing that I realized is that erroneous inputs are not validated. I had to start things all over again. Here's my new code working better now.

from datetime import datetime #Import datetime from datetime module

#Create the main function
def main():
    while True:
        user_input = input("Enter your date: ")
        try:
            print(convert_date(user_input))
            break
        except ValueError as e:
            print(e)

#Create a function to convert user input to 'YYYY-MM-DD' date format
def convert_date(user_input):
    try:
        if '/' in user_input: #Condition to check if text contains '/'
            my_date_obj = datetime.strptime(user_input, "%m/%d/%Y")
        else:
            my_date_obj = datetime.strptime(user_input, "%B %d, %Y")
    except ValueError:
        raise ValueError
    return my_date_obj.strftime("%Y-%m-%d")


main()

You must log in to answer this question.

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