File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ __author__ = 'natezhang93'
4+
5+ '''
6+ 第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,
7+ 当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
8+ '''
9+
10+ class Filter :
11+ def __init__ (self ):
12+ self .data = []
13+ try :
14+ with open ("filtered_words.txt" , 'r' ) as fin :
15+ self .data = (fin .read ().splitlines ())
16+ fin .close ()
17+ except :
18+ raise IOError
19+
20+ def check (self , word ):
21+ try :
22+ self .data .index (word )
23+ result = 'Freedom'
24+ except :
25+ result = 'Human Rights'
26+ return result
27+
28+ def main ():
29+ try :
30+ fil = Filter ()
31+ except :
32+ print "ERROR OPENING FILE"
33+ return
34+
35+ while True :
36+ word = raw_input ("> " )
37+ word = fil .check (word )
38+ print word
39+
40+ if __name__ == "__main__" :
41+ main ()
Original file line number Diff line number Diff line change 1+ 北京
2+ 程序员
3+ 公务员
4+ 领导
5+ 牛比
6+ 牛逼
7+ 你娘
8+ 你妈
9+ love
10+ sex
11+ jiangge
You can’t perform that action at this time.
0 commit comments