第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
解答:(1)读取文件获取所有的敏感词汇;(2)获取输入的内容
#encoding:utf-8
def get_sensitive_words():
words = []
content = open("filtered_words.txt","r",encoding="utf-8")
for line in content:
words.append(line.strip())
return words
if __name__ == '__main__':
words = get_sensitive_words()
print(words)
while True:
input_content = input("请输入内容:")
if not input_content:
break
elif input_content in words:
print("Freedom")
else:
print("Human Rights")
运行效果: