你需要初始化 wordCount = 0 然后在里面 for loop 每次迭代都需要添加到wordcount。像这样的:
wordCount = 0
for loop
wordCount = 0 for line in hand: lin = line.rstrip() wds = lin.split() print(wds) wordCount += len(wds) print(wordCount)