I'm attempting to average the lengths of each word after reading from a file. The text inside the file, however, is not formatted with normal sentence structure. Sometimes there is an extra space between words and new line breaks mid sentence.
Current code
def average(filename):
with open(filename, "r") as f:
for line in f:
words = line.split()
average = sum(len(words) for words in words)/len(words)
return average
>>>4.3076923076923075
Expected
>>>4.352941176470588
File
Here are some words there is no punctuation but there are words what
is the average length
wordsin the code