Activity 2-2
February 28, 2013
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 interactive interpreter window, 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("\n")
stringToSplit.split("a")
stringToSplit.split("out")
Task 2: Count the Number of Words in the Shel Silverstein Poem
- In
ACT2-2.py
, write a function called readShel
that opens the poem and returns a list of words. We have already written each line in class already — you just need to put them together.
- Now fill in the
countWordsInShel()
function.