python - How to select last element of a for loop? -
f = open("unknown.txt", 'r') = sum(line.count('ly') line in f) f = open("unknown.txt", 'r') words = 0 line in f: words += len(line.split()) print(words)
so code outputs numbers leading 78, amount of words in text file, how '78' , '78'? can use itself, in advance!
for line in f: words += len(line.split()) print(words)
this prints total number of words after every line in file. if want print amount of words after python has calculated how many there are, de-indent print
call:
for line in f: words += len(line.split()) print(words)
Comments
Post a Comment