Write a python 3.5 program that can generate the output shown for the .txt file.
ID: 3807702 • Letter: W
Question
Write a python 3.5 program that can generate the output shown for the .txt file. Use proper indentation.
2 Lab Questions l. Write a program called q1.py that analyzes and prints file statistics. First, prompt the user for the name of a file. Then, read and analyze the file and display the following statistics: total number of characters, number of sentences, and number of words. Here are some notes regarding the above statistics you will collect. A character is any symbol in the text file. Examples of characters include letters. numerical digits, punctuation marks, and "invisible" characters such as whitespace and new line characters. Assume a word is separated by whitespace and/or newline characters Assume sentences end in either a period, question mark, or exclamation point Programming tips You'll need to take advantage of the strip() and split f tions for strings. Please see the Topic 3 lecture notes (along with in-class programs) for examples of reading files and stripping and splitting strings. Example #1. The user enters the filename five-letter-words. txt (line 1). The output of the program is then displayed (lines 3 5). Enter a filename: five letter words. txt 3 Characters 16500 Sent ences Words 2750 Example #2. The user enters the file getty sburg.txt e 1). The output of the program is then displayed (lines 3 5). Enter a fil en ame gettysburg .txt 3 Characters 1447 Sentences 10 Words 267Explanation / Answer
#python code
filename = raw_input("Enter a filename: ")
totalSentences = 0
totalWords = 0
totalChars = 0
with open(filename, 'r') as f:
for line in f:
words = line.split()
for word in words:
# increment total lines if we encounter ? or . or !
if word[len(word)-1] in "?.!":
totalSentences += 1
# increment count of words as we iterate thrrough the words list
totalWords = totalWords + 1
# total chars will be all the characters in each line
totalChars = totalChars + len(line)
print "Character:", totalChars
print "Sentences:", totalSentences
print "Words:",totalWords
'''
output:
Enter a filename: input.txt
Character: 217
Sentences: 3
Words: 43
input.txt
what's the point of this? fname is a string first and then a file object. You don't really use the string defined
in the first line and you should use one variable for one thing only: either a string or a file object.
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.