r/PythonLearning • u/No_Safety_5957 • 2d ago
Check50, uhm what?

months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
def split(var):
if "/" in var:
splitted = var.split("/")
if splitted[0].isdecimal() == False:
#print("error in splitting")
return
elif "," in mdy_format:
splitted = var.split(" ")
try:
return splitted
except UnboundLocalError:
#print("unboundlocalerror")
return
def checkvalues(checkval):
day, year = checkval[1], checkval[2]
#print(day)
if checkval[0].isdigit():
month = int(checkval[0])
else:
try:
month = months.index(checkval[0]) + 1
except ValueError:
#print("valueerror")
return
if month > 12:
return
day = day.replace(",","")
#print(day)
try:
day = int(day)
year = int(year)
except ValueError:
return
if day > 31:
#print("moth longer error")
return
#print(f"{day:02}")
return f"{year}-{month:02}-{day:02}"
while True:
mdy_format = input("Date: ")
mdy_splitted = split(mdy_format)
#print(mdy_format)
#print(mdy_splitted)
#print(len(mdy_splitted))
#useless comment
#another useless comment
if not mdy_splitted or len(mdy_splitted) != 3 or checkvalues(mdy_splitted) is None:
#print("error here")
continue
print(checkvalues(mdy_splitted))
breakwhy does it return an error? After doing acheck it seems to return the correct answer.
Code above (ignore the comments)
3
3
2
u/Careful_Exercise_956 2d ago
Input Whitespace (check50 failure): In your screenshot, " 9/8/1636 " failed because leading/trailing spaces weren't stripped
Also in you code, there is a spelling error when you typed "moth" instead of "month"
2
u/MK_OFFICIAL3943 2d ago
Bro, I see the bug - it's in your split function.
You wrote: elif "," in mdy_format:
But it should be: elif "," in var:
Because var is what you passed to the function, mdy_format is the global one from outside. So when you type "September 8, 1636", it never goes into the elif, splitted never gets defined, and then you get UnboundLocalError.
Change mdy_format to var there and it should work.
Also small tip - try printing mdy_splitted before the check, you'll see it directly.
•
u/Sea-Ad7805 2d ago
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.