HW4Q1
user_7676782
python
3 years ago
1.1 kB
12
Indexable
def getLastWord(file):
lstLastWords = []
line = file.readline()
i = 1
while line != '':
# split the sentence to a list
words_list = line.split()
# get the last word
lastWord = words_list[len(words_list) - 1]
# add it to a new list of all last words
lstLastWords.append(lastWord)
# next line
line = file.readline()
i += 1
return lstLastWords
def getAverageWord(file):
lineNum = 0
wordNum = 0
# get the line
line = file.readline()
while line != '':
# split the sentence to a list
words_list = line.split()
# get the number of words in the sentence and adding it to the total
wordNum = wordNum + len(words_list)
# add 1 for sentence
lineNum += 1
# go to the next sentence
line = file.readline()
averageWord = wordNum / lineNum
print(wordNum)
print(lineNum)
print(averageWord)
def main():
fileContent = open('imagine.txt', 'r')
# getAverageWord(fileContent)
getLastWord(fileContent)
main()
Editor is loading...