Python练习册(十一)——查找敏感词

problem0011查找敏感词

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

北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
  • 读取文件获取敏感词,去除多余字符,并存入链表
  • 采用脚本获取输入,遍历对比打印输出

demo:

#!/usr/bin/python3
from sys import argv

scripts,Input = argv

def getFilterwords():
    filterwords = []
    f=open('filterwords.txt')
    for word in f:
        filterwords.append(word[:-1])
    f.close()
    return filterwords

def checkFilterwords(filtWord,Input):
    for w in filtWord:            
        if w == Input:
            print('Freedom')
            return
    print('Human Rights')
    return 

if __name__ == '__main__':
   checkFilterwords(getFilterwords(),Input)

效果:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_30650153/article/details/80879766