Collections and Exceptions
15-112 Lab #4 - due Friday, June 6, 2014


This lab is designed to improve your confidence with Python's collections (Lists, Dictionaries, Sets, Tuples), File and User I/O, and exception handling facility. It is also designed to give you a good opportunity to break a task down into readable functions.

Books, and Related Functions

A book can be represented as a tuple of the author's lastName, the author's firstName, the title, date, and isbn.

Write a function, readBook(), that, given a comma-separated string contain this information, returns a tuple representing the book.

Write a function, readBooks(), that, given the name of a text file containing one comma-separated line per book, uses readBook() to return a list of tuples, each of which describes one book.

Write a function, buildIndex(), that, given a list of books as returned by readBooks(), builds a map from key word to book title. A key word is any word in a book's title, except "a", "an, or "the".


Write a function, lookupKeyword(), that, given a dictionary as returned by buildIndex(), and a one or more keywords in a string, returns a list of the titles of each book that contains at least one key word, sorted by the number of matching keywords.

Main Program

Write a function presentMenuAndGetChoice() that offers the user the option to initialize an index, query an index, or quit, and appropriately prompts the user for additional information, as required. The option to query the index should print the results to the screen, or a meaningful message, if there are no matches.


You should gracefully handle and recover from all naturally occuring exceptions, such as bad input from the user or an inaccesible file.

Should a query have no results, you should raise an exception, NoResultsError within the query function -- and handle it within the main part of the program where lookupKeyword() is called. This, of course, means that you'll need to define a NoResultsError, as this is not a standard Python-defined exception.