打印不含字母"e"的单词

这个例子是在《像计算机科学家一样思考Python》中练习9-2

要求:写一个函数has_no_e,当给定的单词不包含字母“e”时,返回True。

修改前一节练习中的代码,打印出不含“e”的单词,并计算这种单词在整个单词表中的百分比。(words.txt)

文档下载处:http://thinkpython.com/code/words.txt

def has_no_e():
    fin = open('words.txt')
    count_all = 0
    count_no_e = 0
    for line in fin:
        count_all += 1
        if 'e' not in line:
            print(line.strip())
            count_no_e += 1
    percentage = count_no_e/count_all
    print("%.2f%%" % (percentage*100))

has_no_e()

输入结果省略打印出的全部不含“e”的单词。 

33.07%

猜你喜欢

转载自blog.csdn.net/sisqzy86/article/details/82818220
今日推荐