第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
#word.txt 北京 程序员 公务员 领导 牛比 牛逼 你娘 你妈 love sex jiangge
这题很简单的,读入word.txt的内容然后挨个查找输入文字就好。
Code:
def filterwords(x): with open(x, 'r') as f: text = f.read() print (text.split('\n')) userinput = input('myinput:') for i in text.split('\n'): if i in userinput: return True if filterwords('word.txt'): print ('freedom') else: print ('human rights')
Notes:
1. text.split('\n'),将原本txt中的文本以换行符为界限分割成个体形成list:
['北京', '程序员', '公务员', '领导', '牛比', '牛逼', '你娘', '你妈', 'love', 'sex', 'jiangge']