COSC 1306
Spring 2021
Assignment 9: Strings, Functions, and Exception Handling
[1] Objectives: This assignment’s primary purpose is to practice processing strings, handling exceptions,
and to divide tasks into functions. There is the primary data structure you have to pass around the
functions to get the tasks done. Unfortunately, we have not discussed the use of files yet. So you will
have to enter the data manually. You will decide the parameters and local variables to use. Do not use
global variables.
[2] Description: Now that many Americans have received COVID-19 shots, it is time to build a system to
track the vaccination date. Suppose the vaccination data comes in in a $-Separated Values, similar to the
more popular Comma-Separated Values (CSV). Here is a sample:
Doe$Jane$12/02/00$Moderna$03/11/21$04/13/21
Five dollar signs separate six values. The six values represent last name, first name, DoB, Vaccination
product, first V-date, and second V-date. We avoid using CSV because a comma may be used in the
product’s name, such as “Pfizer, Inc,” making CSV fail.
Data structure. The primary data structure you will use is a table (a list of tuples) with some experience
efore. Similar to the last assignment, there is a minor conversion issue. Typically, we use date in the
form of MM-DD-YY, but that is not an international standard, and it makes sorting difficult. You may have
noticed that I prefer the YYYY-MM-DD format, such as XXXXXXXXXXwith or without ‘-‘). You will have to
do the conversion. For years (YY) between 00 and 21, treat them as 20YY; otherwise, make it 19YY. That
is the best we can do. You can get the program to work first and then come back to add this conversion.
I suggest that you do the conversion early in the code. Keep in mind that tuples are immutable, so better
make the change before you put everything into a tuple.
Here is what you need to do.
Build the table by getting the data in DSV format, check for e
or
Print the table
Sort the table based on the date of the second shot.
Print the sorted table.
table = build_table()
print_table(table)
table.sort(key=lambda x:x[5])
print_table(table)
The program is relatively short. One can do a lot by calling li
ary functions to do the work in Python. The
code that I tested is less than 35 lines.
[3] Requirements: I am giving you the main program. One reason is to force you to keep the main
program simple (mainly calling some functions) and organized. A second reason is to give you the lambda
function for the sorting. The third reason is to force you to use sort() instead of sorted().
Use the main program as is. Do not modify it. You should write the functions to make it work.
You will need to write other functions, not just the ones mentioned in the main program.
You will have to check for e
ors or exceptions (try-except) when getting the input. At the
minimum, handle the case when there are less than six values in the input. Keep asking the user
to provide the data.
The input phase ends when the user enters an empty string.
[4] Input and Output: See sample outputs below and the demo in class. To make it easier to test, I have
prepared several strings here so you can copy and paste them into the IDE. These are just for testing the
code. The TA may test it with other strings. Try copy 4 or 5 values and see if your exception handling
works.
Smith$John$02/28/95$Pfizer$01/25/21$02/23/21
Doe$Jane$12/02/00$Moderna$03/11/21$04/13/21
Carter$James$09/01/55$Johnson and Johnson$02/01/21$03/02/21
[5] Deadline: Midnight, Monday, April 19, 2021.
Please enter a record: Smith$John$02/28/95$Pfizer$01/25/21$02/23/21
Please enter a record: Doe$Jane$12/02/00$Moderna$03/11/21$04/13/21
Please enter a record: Carter$James$09/01/55$Johnson and Johnson$02/01/21$03/02/21
Please enter a record:
Last Name First Name DoB XXXXXXXXXXVaccine Product 1st V-Date 2nd V-Date
Smith John XXXXXXXXXXPfizer XXXXXXXXXX XXXXXXXXXX
Doe XXXXXXXXXXJane XXXXXXXXXXModerna XXXXXXXXXX XXXXXXXXXX
Carter James XXXXXXXXXXJohnson and Johnson XXXXXXXXXX-02
Last Name First Name DoB XXXXXXXXXXVaccine Product 1st V-Date 2nd V-Date
Smith John XXXXXXXXXXPfizer XXXXXXXXXX XXXXXXXXXX
Carter James XXXXXXXXXXJohnson and Johnson XXXXXXXXXX-02
Doe XXXXXXXXXXJane XXXXXXXXXXModerna XXXXXXXXXX XXXXXXXXXX