Home | Lecture 3 | Problem 3.1 | Problem 3.2 | Problem 3.3 | Problem 3.4
In the United States, dates are typically formatted in month-day-year order (MM/DD/YYYY), otherwise known as middle-endian order, which is arguably bad design. Dates in that format can’t be easily sorted because the date’s year comes last instead of first. Try sorting, for instance, 2/2/1800, 3/3/1900, and 1/1/2000 chronologically in any program (e.g., a spreadsheet). Dates in that format are also ambiguous. Harvard was founded on September 8, 1636, but 9/8/1636 could also be interpreted as August 9, 1636!
Fortunately, computers tend to use ISO 8601, an international standard that prescribes that dates should be formatted in year-month-day (YYYY-MM-DD) order, no matter the country, formatting years with four digits, months with two digits, and days with two digits, “padding” each with leading zeroes as needed.
In a file called outdated.py, implement a program that prompts the user for a date, anno Domini, in month-day-year order, formatted like 9/8/1636 or September 8, 1636, wherein the month in the latter might be any of the values in the list below:
[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]Then output that same date in YYYY-MM-DD format. If the user’s input is not a valid date in either format, prompt the user again. Assume that every month has no more than 31 days; no need to validate whether a month has 28, 29, 30, or 31 days.
-
Recall that a
strcomes with quite a few methods, per docs.python.org/3/library/stdtypes.html#string-methods, includingsplit. -
Recall that a
listcomes with quite a few methods, per docs.python.org/3/tutorial/datastructures.html#more-on-lists, among which isindex. -
Note that you can format an
intwith leading zeroes with code likeprint(f"{n:02}")
wherein, if n is a single digit, it will be prefixed with one 0, per docs.python.org/3/library/string.html#format-string-syntax.
From the root of your repository execute cd 3-Exceptions So your current working directory is ...
/3-Exceptions $:Next execute
mkdir outdatedto make a folder called outdated in your codespace.
Then execute
cd outdatedto change directories into that folder. You should now see your terminal prompt as /3-Exceptions/outdated $. You can now execute
code outdated.pyto make a file called outdated.py where you’ll write your program.
Here’s how to test your code manually:
-
Run your program with
python outdated.py. Type9/8/1636and press Enter. Your program should output:1636-09-08
-
Run your program with
python outdated.py. TypeSeptember 8, 1636and press Enter. Your program should output:1636-09-08
-
Run your program with
python outdated.py. Type23/6/1912and press Enter. Your program should reprompt the user. -
Run your program with
python outdated.py. TypeDecember 80, 1980and press Enter. Your program should reprompt the user.
At the /3-Exceptions/outdated $ prompt in your terminal:
git add -A Add all changed files in the repository to be committed
git commit -m “Upload completed outdated.py“Commit all changes in the REPO with the comment “Upload completed outdated.py“ note: If the file is not complete, adjust the comment to describes what is being commited
git push Push all changes to the REPO