I am working on the "outdated" problem.
I need to reformat the output given by the user in order to adehere to the ISO format.
My code is as follows:
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
months_and_number = {"January":"01", "February":"02", "March":"03", "April":"04", "May":"05", "June":"06", "July":"07", "August":"08", "September":"09", "October":"10", "November":"11", "December":"12"}
while True:
try:
x = input("Date: ")
test_coma = x.index("," or "/")
except ValueError:
x = input("Date: ")
else:
if "," in x:
split_coma = x.split(",")
if len(split_coma) <2 and len(split_coma) > 2:
x = input("Date: ")
else:
if len(split_coma[1]) < 4 or len(split_coma[1]) > 4:
x = input("Date: ")
else:
year = split_coma[1]
if months not in split_coma[0]:
x = input("Date: ")
else:
split_space = split_coma[0].split(" ")
if split_space[0] in months_and_number:
months = months_and_number[split_space[0]]
else:
x = input("Date: ")
if len(split_space[1]) < 2:
day = "0" + split_space[1]
else:
day = split_space[1]
iso = year + "-" + months + "-" + day
print(iso)
else:
y = x.split("/")
if len(y) < 3 and len(y) > 3:
x = input("Date: ")
else:
if len(y[0]) < 2:
a = "0" + y[0]
else:
a = y[0]
if len(y[1]) < 2:
b = "0" + y[1]
else:
b = y[1]
c = y[2]
new_date = y[2] + "-" + y[0] + "-" + y[1]
print(new_date)
I know that it is far from perfect, with a lot of conditions within conditions, but still it should work. But it does not.
I am always prompted to give a date after another, indefinitely. Why?
=======================
Here is the updated code, based on your help:
list_months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
list_day_months = ["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]
dic_months_numb = {"January": "1", "February":"2", "March":"3", "April":"4", "May":"5", "June":"6", "July":"7", "August":"8", "September":"9", "October":"10", "November":"11", "December":"12"}
** Process exited - Return Code: 0 **
Press Enter to exit terminal]
date = input("Date ")
def isValid(value):
if "," or "/" not in value:
return False
else:
if "," in value:
return isValidComma(value)
else:
return isValidSlash(value)
def isValidComma(value):
"""Determines if value is a valid date in comma format"""
# reminder comma format is "month_as_word (D)D, YYYY"
# perform needed checks here
check_coma = value.split()
month = check_coma[0]
day_comma = check_coma[1]
year = check_coma[2]
day = day_comma.replace(",", "")
if (month not in list_months) or (len(day) > 2 and day not in list_day_months) or (len(year) > 4 and year.isnumeric() = False ):
return False
def isValidSlash(value):
"""Determines if value is a valid date in Slash format"""
# reminder slash format is "(M)M/(D)D/YYYY"
# perform needed checks here
check_slash = value.split("/")
month = check_slash[0]
day = check_slash[1]
year = check_slash[2]
if (not 1 < month < 12) or (not 1 < day < 31) or (not 1 000 < year < 10 000)
return False
def convertFromComma(value):
"""returns proper year, month and day from the comma based value"""
# reminder comma format is "month_as_word (D)D, YYYY"
for month in dic_months_numb:
month_numb = dic_months_numb[month]
return year, month, day
#**I start to have significant issue here. How is the function going to "know" what to take? What will "value" refer to?**
def convertFromSlash(value):
"""returns proper year, month and day from slash based value"""
#do stuff
return year, month, day
# Main Code
valid = False
while not valid: #3
try:
x = input("Date: ") #1
valid = isValid(x): #2
except ValueError:
valid = False
#end of try block
#while block completed
#now determine which format it is and convert it
#4
if ',' in x:
year, month, day = convertFromComma(x)
else:
year, month, day = convertFromSlash(x)
print(f"{year:04}-{month:02}-{day:02}")
====================
Here is a further update:
def isValidSlash(value):
"""Determines if value is a valid date in Slash format"""
month, day, year = value.split("/")
if (1 < int(month) or int(month) > 12) or (1 < int(day) or int(day) > 31) or (1000 < int(year) or int(year) > 10000):
#I had an error here when testing, telling me that the operator "<" cannot be used with "int" and "str", which makes sense. I thus converted day, month and year as string, but this is still not correct. I do not get an error, but when inputting a date I get re prompted again and again.
return False
else:
return True
def convertFromComma(value):
"""returns proper year, month and day from the comma based value"""
# reminder comma format is "month_as_word (D)D, YYYY"
month, day, year = value.split()
day = day.rstrip(',')
month = months[month]
#I added the look up in the dictionary, because the month is given with letter and we would like to look up the number of the month.
return year, month, day
I also changed the f string in the end, to have a zero "leading" (in front? Padding) rather than added at the back:
print(f"{year:04}-{month:>02}-{day:>02}")
test_coma
is your own variable you create but never use