Activity 2-2
March 1, 2016
Task 1: Review from Last Class
- On the Desktop, make a folder called
ACT2-2. Download ACT2-2.py, poem.txt, and MobyDick.txt, and save them in the ACT2-2 directory.
- Open
IDLE, and then open ACT2-2.py. Remind yourself of what the two functions actually do.
- In the
ACT2-2 window, press F5 (or select Run > Run Module). What happens?
- In the IDLE Python shell that should have opened when you pressed F5, assign the list
[-5,0,-2] to the variable myList. Use the avg3() function to compute the average.
- Use the
addOne() function to add one to myList[2].
- The
split function, without an input argument, splits a string on any whitespace (including spaces, newlines, tabs, etc.). Use the split() function to split the variable stringToSplit on different delimiters. Observe what happens when you enter the following expressions:
stringToSplit.split()
stringToSplit.split("\n")
stringToSplit.split("a")
stringToSplit.split("out")
Task 2: Read in the Shel Silverstein Poem, and split it into words
- In
ACT2-2.py, write a function called readShel that opens the poem and returns a list of words.
- Download the Shel Silverstein poem, and assign the file location as a string to a variable named
fileName. Write an expression that opens the file at fileName for reading and assigns the return value to myFile. The value of myFile now a file object that has been opened and can be read.
- Assign to
fileString the contents of the file, which are returned by calling the read function on the file. fileString is now a big string that contains the poem.
- Close the file object
myFile; you don't need it any longer, and the operating system really hates when you forget to close a file you've opened.
- Use the
split function to create a list of words from the string, separated by whitespace, and return that list.
Task 3: Count the Number of Words in the Shel Silverstein Poem
- Now fill in the
countWordsInShel() function.
- Grab the list of words in the poem using the
readShel function you wrote.
- Create a variable with the value 0 that will keep track of your count.
- Use a for-loop to iterate over all words, incrementing your count each time.
- Return the count when the for-loop is finished - it should have been incremented once for each word in the poem!
- Repeat the previous two steps using "Moby Dick" instead of the poem. Fill in the functions
readMobyDick and countWordsInMobyDick. They should be very similar to the functions you just wrote.
If we have time...
Task 4: Compute the Average Word Length in "Moby Dick"
- Fill in the
avgWordLengthInMobyDick() function.
- Grab the list of words in the book using the
readMobyDick function you wrote.
- Create a variable with the value 0 that will sum up the lengths of all words.
- Use a for-loop to iterate over all words, summing up the length of all words using the
len function.
- With the total sum of all word-lengths (i.e., the total number of non-whitespace characters), get the average by dividing this sum by the number of words and return the average.