天天一个小程序—0011题(敏感词检测)

第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为如下内容,当用户输入敏感词语时,则打印出 Freedom,不然打印出 Human Rights。python

 

 1 #!/usr/bin/env python
 2 
 3 filtered = []  4 
 5 def get_filtered_words():  6     f = open('test.txt')  7     for word in f.readlines():  8         if '\n' in word:  9             filtered.append(word[:-1]) 10         else: 11  filtered.append(word) 12     print(filtered) 13 
14 def input_check(): 15     while True: 16         text = input('请输入内容:') 17         if text in filtered: 18             print( 'Freedom') 19         else: 20             print('Human Rights') 21 
22 if __name__ == '__main__': 23  get_filtered_words() 24     input_check()

相关文章
相关标签/搜索