0

Assignment Overview1 This assignment focuses on the design, implementation and testing of a Python program which uses file processing, lists and strings to solve the problem described below. Program Specifications: The New York Times newspaper has published “best seller” lists since 1942. Book sales are tracked nationwide, leading to a list of those books which have recently sold the most copies. You will design, implement and test a program which allows the user to search a subset of the books which have appeared in the New York Times best seller lists. For simplicity, the data set will only contain those books which have reached #1 on either the fiction or nonfiction list since 1942.
The file named bestsellers.txt contains the data set. Each line of the file contains the information for a separate book, which includes: title, author, publisher, date it first reached #1 on one of the best seller lists, and category (fiction or nonfiction). There is a tab character between fields.
The program will input the data set and construct a list of books. Code for reading the contents of a text file into a list and splitting this list into a list of lines is provided in the helper.py file.
After constructing the list of books, the program will prompt the user for two years (a starting year and an ending year), then display all books which reached the #1 spot between those two years (inclusive). For example, if the user entered “1970” and “1973”, it will display all books which reached #1 in 1970, 1971, 1972 or 1973.
Input validation should be performed on user inputs. The only valid inputs are the beginning year, ending year (range 1942-2014) and ‘q’ or ‘Q’ for quit. User should be prompted until a valid input is received.

So far this is what I've gotten:

openBook = open('bestsellers.txt')
Lst = openBook.read()
list1 = Lst.split('\n')

global beginYear
global endYear

def validateYear():
    while True:
        try:
        # User inputs beginning and ending year and also validates the input
            beginYear = input("Enter a beginning year (enter 'q' or 'Q' to quit): ")
            if beginYear == 'q' or beginYear == 'Q':
                print("Thank you!")
                break
            beginYear = int(beginYear)
        except ValueError:
            continue
        if beginYear < 1942 or beginYear > 2014:
            print("Year must be in the range 1942-2014.")
            continue
        endYear = input("Enter an ending year: ")
        endYear = int(endYear)
        if endYear < 1942 or endYear > 2014:
            print("Year must be in the range 1942-2014.")
            continue
    return(beginYear, endYear)

I had a code to try and print the list I wanted it to search for but instead it printed the whole text file instead of the lines within the 2 years.
I want the program to acknowledge the begin and end year, search for those years in the text file, and print the lines within the two years the user had input.

Here is a pseudocode:
Algorithm in pseudocode :

Open bestseller file as inputFile 
Read inputFile data into list bookList 
Split bookList into a list of strings with each string storing one line of bestseller file 
Loop  
    Prompt user for beginning year 
        If input is equal to ‘q’ or ‘Q’  
            Terminate loop 
        Validate beginning year 
        Prompt user for ending year 
        Validate ending year
        For each string in list bookList do 
            Split string into words using tab as the delimiter 
            For each word in string do If word contains ‘/’ then
                Split word into month, day and year using ‘/’ as the delimiter 
                If year in the range start year to end year inclusive then 
                    Display string 

  End loop 

And here is an example:

Enter beginning year (‘q’ or ‘Q’ for quit): 1960 
Enter ending year: 1962 
All Titles between 1960 and 1962 
A Shade of Difference Allen Drury Doubleday 10/28/1962 Fiction 
Hawaii James Michener Random House 1/17/1960 Fiction 
Seven Days in May Fletcher Knebel Harper 11/18/1962 Fiction    
Ship of Fools Katherine Anne Porter Little, Brown 4/29/1962  Fiction 
The Agony and the Ecstasy Irving Stone Doubleday 4/23/1961 Fiction 
The Last of the Just Andre Schwarz-Bart Atheneum 3/26/1961 Fiction 
Born Free Joy Adamson Pantheon 8/7/1960 Nonfiction 
Calories Don't Count Herman Taller Simon & Schuster 3/25/1962 Nonfiction 
May This House Be Safe from Tigers Alexander King Simon & Schuster 3/13/1960 Nonfiction    
Silent Spring Rachel Carson Houghton Mifflin 10/28/1962 Nonfiction 
The Making of the President - 1964 Theodore H. White Atheneum 8/1/1965 Nonfiction 
The New English Bible Oxford University Press (Editor) Oxford University Press 5/28/1961 Nonfiction 
The Rise and Fall of the Third Reich William Shirer Simon & Schuster 12/4/1960 Nonfiction 
The Rothchilds Frederic Morton Atheneum 6/24/1962 Nonfiction 
The Waste Makers Vance Packard McKay 11/6/1960 Nonfiction 
Travels with Charley John Steinbeck Viking 10/21/1962 Nonfiction  
Enter beginning year (‘q’ or ‘Q’ for quit): Q

I've been trying this for a couple days now and still my mind is still blank.
Thank you for your co-operation.

4
Contributors
3
Replies
42
Views
1 Year
Discussion Span
Last Post by jacks009
0

Open the file with:

openBook = open('bestsellers.txt')
Lst = openBook.read()
list1 = lst.split('\r') # split carriage returns

Try this:

def find_books(book_list, begin_year, end_year):

    for book_info in book_list:

        # break up each word of the book entry
        book_items = book_info.split()

        # the 2nd to last item is the date the book first reached #1 
        the_date = book_items[-2]

        # the last four charters of the date is the year
        the_year = int(the_date[-4:])

        if the_year >= begin_year and the_year <= end_year:
            print book_info

find_books(list1, beginYear, endYear)

Edited by Nick_14: typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.