Here is my code
def getInputFile ():
bad = True
while bad:
try:
fileName = input ("Enter file name: ")
# Open file for input
f = open(fileName, "r") # Note: "r" means open for reading.
bad = False
except Exception as err:
print ("Please enter a valid file name:")
return f
lines=0
wordCount=0
fileHandler=getInputFile()
for lineOfText in fileHandler.readlines():
lines += 1
print(str(lines),str(lineOfText))
f1=lineOfText.split()
wordCount=wordCount+len(f1)
print ("Word count:" +str(wordCount))
Currently, my program counts only the running total of words in the text file but I want it to just count the words in each line of the file. Also I would like the program to analyze the text file at the end and print out things such as "most words in a line" and "average words per line" but I can't do that with my current format. Any help would be greatly appreciated.