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)
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)
Step by step
Solved in 2 steps with 1 images