r/PythonLearning • u/No_Safety_5957 • 4d 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)
8
Upvotes
•
u/Sea-Ad7805 4d ago
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.
1
u/No_Safety_5957 1d ago
Thanks to all of you!
3
u/Outside_Complaint755 4d ago
Look very carefully at the input in the failing test case.
3
u/ajamdonut 4d ago
strip()
2
u/Careful_Exercise_956 4d 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"
1
u/MK_OFFICIAL3943 4d 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
varis what you passed to the function,mdy_formatis the global one from outside. So when you type "September 8, 1636", it never goes into the elif,splittednever gets defined, and then you get UnboundLocalError.Change
mdy_formattovarthere and it should work.Also small tip - try printing
mdy_splittedbefore the check, you'll see it directly.