Week 5 Assignment Exercise 1 Problem 7

Solution given in assignment given in week 5 seems to produce wrong answer

no_of_words = 0
with open(‘sample text.txt’, ‘r’) as file:
for line in file.readlines():
no_of_words += line.len()
no_of_words

produces 467 instead of 73 words

def no_of_words(input_file):
with open(input_file,“r”) as f:
words=f.readlines()
length=len(words)
s=["\n",",",".",";",":","!"]
for i in range(length):
for t in s:
words[i]=words[i].replace(t,’ ')
new_file = " ".join(words)
new_words=new_file.split()
new_length= len(new_words)
return new_length

no_of_words(“sample.txt”) returns
73

Please check this:

For further queries, please write there.

I think answer should be 79 instead of 467. Here is my code:

sample_text = "Quantum entanglement is a label for the observed physical phenomenon\nthat occurs when a pair or group of particles is generated, interact, or share\nspatial proximity in a way such that the quantum state of each particle\nof the pair or group cannot be described independently of the state of the others,\neven when the particles are separated by a large distance. The topic of\nquantum entanglement is at the heart of the disparity between\nclassical and quantum physics."
with open('sample text.txt','w') as file:
    file.write(sample_text)
with open('sample text.txt','r') as file:
    lines=file.readlines()
    num=list()
    for line in lines:
        rev=line.replace(","," ")  #replacing , by white space in every line
        rev1=rev.replace("."," ")  #replacing . by white space in every line
        words=rev1.split()  #list containing separate words from every line
        num.append(len(words))
num
-> [10, 14, 13, 15, 13, 10, 4]  #output
sum(num)
-> 79

It returns 79 as output.