最近静沉下心来复习python基础,选了一些基础题试一试
第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge
f = open(r'D:\python小游戏\敏感文件.txt','r',encoding='utf-8') ##打开文件
content = f.read() ##读取文件内容
while True:
new_word = input("请输入测试敏感词汇:")
if new_word in content: ##通过in判断,输入的词是否在content里面
print("Freedom")
else:
print("Human Rights")
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
f = open(r'D:\python小游戏\敏感文件.txt','r',encoding='utf-8') ##打开文件
content = f.read() ##读取文件内容
cons = content.split('\n') ##将每个关键字分割成列表
while True:
word = input('请输入:')
for x in cons:
if x in word:
new_word = word.replace(x,"****") ##判断关键字是否在输入的字符串里,如果有就替换
break
else:
new_word = word
print(new_word)