Lab Exercise 10

Writing and Using a Module

CS 1510
Introduction to Computing


Create a directory on your USB device for this lab, say, lab10, and launch IDLE.

You will need two text files (plays by Shakespeare!), a Python program, a partially-completed Python program, and a responses.txt file to do today's lab. Download this zip file and place the contents of the directory into your lab10 directory.

At the end of the lab, you submit your responses.txt file along with the programs you wrote.

Task 1: Write Some String Utilities

Sometimes, we like to have functions that let us write our code in shorthand. String processing is one of those times.

Write the following string-processing functions.

starts_with(word, prefix)

Returns true if word starts with prefix, and false otherwise.
    >>> starts_with('alas', 'A')
    >>> starts_with('Alas', 'a')
is_length_of(word, number)

Returns true if word has exactly number characters, and false otherwise.
    >>> is_length_of('Eugene', 6)
    >>> is_length_of('Mother Night', 6)

Returns true if word reads exactly the same forward and backward, and false otherwise.
    >>> is_palindrome('Eugene')
    >>> is_palindrome('racecar')

Name your file

Task 2: Use Your str_utils Module

The file contains only function definitions, so if we run the file, nothing observable happens.

Instead, we can import the functions by treating the file as a module! For example:

    >>> import str_utils
    >>> str_utils.starts_with('Alas', 'a')
    >>> str_utils.is_palindrome('Eugene')

We can also import individual functions ans use them directly with the from...import... construction:

    >>> from str_utils import is_length_of
    >>> is_length_of('Mother Night', 12)

(Cool, huh?)

Next, run the program on the text files for Macbeth and Midsummer Night's Dream. Use a number of different word lengths for each.

Copy the results into your responses file.

Task 3: Extend the Text Analysis Program

Directly comparing the word counts between Macbeth and Midsummer Night's Dream is misleading, because the plays have different numbers of words.

Make a copy of called

Modify so that it reports the percentage of the words that are of the target length.

For example:

    Enter name of text file : macbeth.txt
    Enter target word length: 7
    In the play in file macbeth.txt,  7.53% of the words are 7 characters long.

Task 4: Extend the Text Analysis Program Again

Now we can compare the word-length counts of Macbeth and Midsummer Night's Dream. Most authors tend to use similar word types whatever they write. Do these two plays have similar percentages for different word lengths?

Fill in the blanks in the partially-completed program

It moves the computation of the percentage of the words that are of the target length into a function named percentage_for_play(filename, word_length). When complete, the program runs like this:

    Enter name of one file    : macbeth.txt
    Enter name of another file: midsummer-nights-dream.txt
    Enter target word length  : 7

    words of length  7
     7.53%   macbeth.txt
     7.36%   midsummer-nights-dream.txt

Use the code from as your starting point. You may copy code from and paste it into

Task 5: The Challenge Round

If you get this far in lab, give yourself a standing ovation.

(Seriously. Do it now.)

As it stands, comparing Macbeth and Midsummer Night's Dream across a range of word-length counts requires us to run multiple times and look for trends in the differences between the percentages. A program can do that for us.

Extend the word comparison program to compare two plays for all word lengths up to and including the value entered by the user.

Modify your output so that it shows the difference between the two percentages at each word length. I'm thinking something like this.

Name your file

Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab10 -- Writing and Using a Module.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.

Eugene Wallingford ..... ..... October 29, 2014