我正在计算文件中的字数,然后我想计算这些数字。现在,这个代码只保存最后一行代码。我怎样才能让它工作?你知道吗def fileVerify():
start = 0
while start == 0:
fileName = input("Please enter the name of the file you want to open ")
try:
inFile = open(fileName, "r")
inFile.close()
start = 1
except Exception as ex:
print("Could not open ", fileName)
print(" Actual exception error message is:")
print(" "+str(ex))
return fileName
def readFile(fileName):
inFile = open(fileName, "r")
count = 1
for lineOfText in inFile:
print(count,": ",lineOfText,end="")
count = count + 1
print(" >>", wordCount(lineOfText), "words")
return lineOfText
def wordCount(sentence):
wordCount=0
sentence=sentence.strip( )
for i in range (0, len(sentence)):
if (sentence[i]==" "):
wordCount=wordCount+1
if (len(sentence)>0):
wordCount=wordCount+1
return wordCount
def wordAverage(a,b):
average = a/b
print("The average words per lines are",average)
def minWords(x,y,z):
if x<=y and x<=z:
print("Least words in a line:", x)
elif y<=x and y<=z:
print("Least words in a line:", y)
else:
print("Least words in a line:", z)
def maxWords(x,y,z):
if x>=y and x>=z:
print("Most words in a line:", x)
return x
elif y>=x and y>=z:
print("Most words in a line:", y)
return y
else:
print("Most words in a line:", z)
return z
def totalWords(x,y,z):
total=x+y+z
print("Total words in input: ", total)
def totalLines(fileName):
inFile = open(fileName, "r")
count = 1
for lineOfText in inFile:
count = count + 1
inFile.close()
return count
这是我的主要任务。我试图让x成为第一个输入,y成为下一个,z成为最后一个。你知道吗#main
print("Welcome to file analysis")
fileName=fileVerify()
sent=readFile(fileName)
x=wordCount(sent)
y=wordCount(sent)
z=wordCount(sent)
print("\nAnalysis")
print("===========")
minWords(x,y,z)
a=maxWords(x,y,z)
b=totalLines(fileName)
wordAverage(a, b)
totalWords(x,y,z)