第 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()