Exercise 3

In class, we have started writing a script that processes two hard-coded sentences, and keep a running total of the number of words actually uttered by the speakers. We did this in a dumb way: copy-pasting the processing block. Now simplify the script using a "for loop". The idea should be to replace the two copies of the processing block with a single copy inside a loop. To do this, you'll need to create a list containing the two sentences, write a "for loop"" operating over this list, and put the processing block inside that loop. You can of course have more than 2 sentences in the list ;-)

Here is what the code looks like so far (also posted on the website):

# initialization
# keep track of total of words
totalWords = 0


# processing of sentence 1
sentence = "B-f: I'm in graduate school"

# get the words of the sentence
words = sentence.split()

print("Words of sentence 1:", words)

# extract speaker ID
speaker = words[0]

# extract words uttered (everyting except first element in the list)
actualWords = words[1:]

# number of words uttered
numberAWords = len(actualWords)

# increment the total
totalWords = totalWords + numberAWords

print("speaker is: ", speaker)
print("words are", actualWords)
print("number of words:", numberAWords)
print("total so far:", totalWords)

# processing of sentence 2
sentence = "A-f: at OSU?"

# get the words of the sentence
words = sentence.split()

print("Words of sentence 2:", words)

# extract speaker ID
speaker = words[0]

# extract words uttered (everyting except first element in the list)
actualWords = words[1:]

# number of words uttered
numberAWords = len(actualWords)

# increment the total
totalWords = totalWords + numberAWords

print("speaker is: ", speaker)
print("words are", actualWords)
print("number of words:", numberAWords)
print("total so far:", totalWords)

# post-analysis
print("total words uttered:", totalWords)