Skip to content

Commit 9324439

Browse files
authored
pset3 q4 code!!
1 parent 1e3f18c commit 9324439

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

outdated/outdated.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
month = [
2+
"January",
3+
"February",
4+
"March",
5+
"April",
6+
"May",
7+
"June",
8+
"July",
9+
"August",
10+
"September",
11+
"October",
12+
"November",
13+
"December"
14+
]
15+
16+
while True:
17+
date = input("Date: ").strip()
18+
try:
19+
m, d, y = date.split('/')
20+
m = int(m) # which means that if 'm' is a string it will produce ValueError
21+
except ValueError:
22+
pass
23+
else:
24+
if int(d)<32 and m < 13:
25+
print(f"{y.zfill(4)}-{m:02}-{d.zfill(2)}", end="")
26+
break
27+
28+
try:
29+
temp1, y = date.split(', ')
30+
m, d = temp1.split()
31+
m = str((month.index(m))+1) #if 'm' is not in the list "month" then it will producde a KeyError
32+
except (ValueError, KeyError):
33+
pass
34+
else:
35+
if int(d) < 32 and int(m) < 13:
36+
print(f"{y.rjust(4, '0')}-{m.rjust(2, '0')}-{d.rjust(2, '0')}", end="")
37+
# we either use (only for str) rjust(2, '0') or .zfill(2) OR (for int and for str will pad 0's to the right not left) f"{example:02}"
38+
break
39+
40+
41+
42+
43+
# i know i can tidy it up further but keeping a single try/exception/else but i already spent 5 hours on this question and finally figured out the October/9/1701 testcase error by converting str to int to get ValueError exception and i am out of patience

0 commit comments

Comments
 (0)