Adding another answer based on your new code. I'm giong to make some changes in that code and point out my changes with comments.
NOTE THIS ANSWER WILL BE DELETED SHORTLY
# removed list_months you don't need it!
# renamed because the name is ugly
# also removing 0 because 0 is not a valid day of a month, added 31 cuz its valid
days = ["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","31"]
# reformatting this to be legible
# renaming to be intelligent
months = {
"January" : "1",
"February" : "2",
"March" : "3",
"April" : "4",
"May" : "5",
"June" : "6",
"July" : "7",
"August" : "8",
"September" : "9",
"October" : "10",
"November" : "11",
"December" : "12"
}
#removed don't do this yet! NO WE ARE DEFINING FUNCTIONS
#date = input("Date ")
def isValid(value):
# rewrite cuz this is wrong and silly
if "," in value:
return isValidComma(value)
elif "/" in value:
return isValidSlash(value)
else
return False
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
# do this properly, split on spaces to 3 values
month, day, year = value.split()
# remove the trailing comma from the day
day = day.rstrip(',') # this will strip ',' from the end (hence the r)
# notice the rewrite and regrouping and changing
if (month not in months) or (len(day) > 2) or (day not in days) or (len(year) > 4) or not year.isnumeric():
return False
# YOU MISSED THE RETURN TRUE
else
return True
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
# DO THIS IN ONE STEP
month, day, year = value.split("/")
#if (not 1 < month < 12) or (not 1 < day < 31) or (not 1 000 < year < 10 000)
# rewrite your logic becuase its broken and wrong
if 1 < month or month > 12 or 1 < day or day > 31 or 1000 < year or year > 10000:
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"
#### COMPLETELY WRONG this doesnt make sense at all
#for month in dic_months_numb:
#month_numb = dic_months_numb[month]
# where did year month and day come from???
# return year, month, day
#recall this logic from before but now we know its all good we dont have to check it
month, day, year = value.split()
# remove the trailing comma from the day
day = day.rstrip(',') # this will strip ',' from the end (hence the r)
# return in the order expected!
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?**
# THE FUNCTION DOENST KNOW WHAT TO TAKE IT, IT TAKES WHAT WE GIVE IT. WHICH WILL
ONLY EVER BE SLASH FORMATTED DATES!! JUST LIKE convertFromComma only gets comma-formatted dates
def convertFromSlash(value):
"""returns proper year, month and day from slash based value"""
# you already checked that its validly / formatted so just grab the values and return them properly
month, day, year = value.split("/") # from above!
return year, month, day
# Main Code
valid = False
while not valid: #3
try:
x = input("Date: ")
valid = isValid(x)
except ValueError:
valid = False
#end of try block
#while block completed
#now determine which format it is and convert it
if ',' in x:
year, month, day = convertFromComma(x)
else:
year, month, day = convertFromSlash(x)
print(f"{year:04}-{month:02}-{day:02}")