am having problems with the wrong output.  the numbers are wrong can someone tell me where is wrong, please if __name__ == '__main__': print('Enter name of file:') name_of_the_file = input() print('File to be processed is:', name_of_the_file) punc = ',', '.', "'", ':', ';', '?', '!', '\\', '"', '(', ')' total_len = 0 count = 0 with open(name_of_the_file, 'r+') as files: values = files.readlines() for i in values: lines = i.rstrip('\n') lines = lines.replace(',', '').replace('.', '').replace("'", '').replace(':', '').replace(';', '')\ .replace('?', '').replace('!', '').replace('\\', '').replace('"', '').replace('(', '').replace(')', '')\ .replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '')\ .replace('6', '').replace('7', '').replace('8', '').replace('9', '') lines = lines.split(' ') for word in lines: word = word.replace(' ', '') total_len = total_len + int(len(word)) count = count + 1 print('The number of words is:', end=' ') print(count) print('The average length of a word is:', end=' ') print(total_len/count)

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter8: I/o Streams And Data Files
Section8.3: Random File Access
Problem 6E
icon
Related questions
Question
100%

i am having problems with the wrong output.  the numbers are wrong

can someone tell me where is wrong, please

if __name__ == '__main__':
print('Enter name of file:')
name_of_the_file = input()
print('File to be processed is:', name_of_the_file)
punc = ',', '.', "'", ':', ';', '?', '!', '\\', '"', '(', ')'
total_len = 0
count = 0
with open(name_of_the_file, 'r+') as files:
values = files.readlines()
for i in values:
lines = i.rstrip('\n')
lines = lines.replace(',', '').replace('.', '').replace("'", '').replace(':', '').replace(';', '')\
.replace('?', '').replace('!', '').replace('\\', '').replace('"', '').replace('(', '').replace(')', '')\
.replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '')\
.replace('6', '').replace('7', '').replace('8', '').replace('9', '')
lines = lines.split(' ')


for word in lines:
word = word.replace(' ', '')
total_len = total_len + int(len(word))
count = count + 1

print('The number of words is:', end=' ')
print(count)
print('The average length of a word is:', end=' ')
print(total_len/count)

8.11 LAB: Processing Text Files
In this exercise, you will build a program to process files containing paragraphs. The aim of the program is to simply count the number
of words and compute their length. The file, however, may contain numbers (e.g. "2018") and contains punctuation that should be
ignored.
There are two data files that you will use to test your program: shortpar.txt and cassini.txt.
(1) Prompt the user to enter the name of a file. Store the text in a string and output the name of the file. (4 pts)
You should prompt the user for the name of the file:
Enter name of file:
shortpar.txt
File to be processed is:
shortpar.txt
(2) Now create the program to read the lines of the file, split each line into words, count the words and total their length. You should
skip any numbers that are in the file and strip any punctuation from the words. Consider the following characters as punctuation ,
: : ? ! \ " (). Some of the words in the files may contain apostrophes (') but ignore them for this part of the exercise. For
example, "NASA's" would be counted as a single word of length 6. You should print out the number of words and their average
length. (8 pts).
For the file shortpar.txt, the output would be:
Enter name of file:
shortpar.txt
File to be processed is:
shortpar.txt
The number of words is:
The average length of a word is:
4.33
Transcribed Image Text:8.11 LAB: Processing Text Files In this exercise, you will build a program to process files containing paragraphs. The aim of the program is to simply count the number of words and compute their length. The file, however, may contain numbers (e.g. "2018") and contains punctuation that should be ignored. There are two data files that you will use to test your program: shortpar.txt and cassini.txt. (1) Prompt the user to enter the name of a file. Store the text in a string and output the name of the file. (4 pts) You should prompt the user for the name of the file: Enter name of file: shortpar.txt File to be processed is: shortpar.txt (2) Now create the program to read the lines of the file, split each line into words, count the words and total their length. You should skip any numbers that are in the file and strip any punctuation from the words. Consider the following characters as punctuation , : : ? ! \ " (). Some of the words in the files may contain apostrophes (') but ignore them for this part of the exercise. For example, "NASA's" would be counted as a single word of length 6. You should print out the number of words and their average length. (8 pts). For the file shortpar.txt, the output would be: Enter name of file: shortpar.txt File to be processed is: shortpar.txt The number of words is: The average length of a word is: 4.33
name_of_the_file = lnput()
print('File to be processed is:', name_of_the_file)
punc = ',',
':', ';,
'?', '!', '\l', '"', '(', ')'
total_len = 0
count = 0
with open(name_of_the_file, 'r+') as files:
values = files.readlines()
for i in values:
lines = i.rstrip('\n')
lines = lines.replace(',', '").replace('.', "').replace("'", '').replace(':', '').replace(';', '')\
.replace('?','').replace('!', '').replace('1l', '').replace('"', '').replace('(', '').replace(')', '')\
.replace('0', ').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '')\
.replace('6','').replace('7', '').replace('8', '').replace('9', '')
lines = lines.split(' ')
for word in lines:
word = word.replace(' ', ')
total_len = total_len + int(len(word))
count = count + 1
print('The number of words is:', end='
print(count)
print('The average length of a word is:',
end=
print(total len/count)
Transcribed Image Text:name_of_the_file = lnput() print('File to be processed is:', name_of_the_file) punc = ',', ':', ';, '?', '!', '\l', '"', '(', ')' total_len = 0 count = 0 with open(name_of_the_file, 'r+') as files: values = files.readlines() for i in values: lines = i.rstrip('\n') lines = lines.replace(',', '").replace('.', "').replace("'", '').replace(':', '').replace(';', '')\ .replace('?','').replace('!', '').replace('1l', '').replace('"', '').replace('(', '').replace(')', '')\ .replace('0', ').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '')\ .replace('6','').replace('7', '').replace('8', '').replace('9', '') lines = lines.split(' ') for word in lines: word = word.replace(' ', ') total_len = total_len + int(len(word)) count = count + 1 print('The number of words is:', end=' print(count) print('The average length of a word is:', end= print(total len/count)
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning