我已经有这个代码:
f = open("unknown.txt", 'r')
a = sum(line.count('ly').endswith() for line in f)
words = 0
with open("unknown.txt", 'r') as f:
words = len(f.read().split())
try:
percentage = a/words*100
print('{}% adverbs'.format(percentage))
except:
print('File is empty!')
但这所做的只是检查一个单词中是否有 'ly',我该如何让它只算作 'ly' if .endswith('ly') (我猜这些命令将被使用,但是我不知道怎么做。有人可以让我的代码这样做吗?提前谢谢!