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