"""
问题:敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
"""deffilterwords():
words = []
f = open('d://filtered_words.txt', 'rb')
for l in f.readlines():
words.append(l.decode('utf-8'))
iw = input('enter your words: ')
for w in range(len(words)):
if iw.find(words[w].strip()) > -1:
print('Freedom')
else:
print('Human Rights')
if __name__ == '__main__':
filterwords()
"""
问题:敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当
用户输入敏感词语,则用 星号 * 替换,
例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
"""deffilterwords(iw):
words =[]
file = open('d://filtered_words.txt', 'rb')
for f in file.readlines():
words.append(f.decode('utf-8'))
for i in range(len(words)):
word = words[i].strip()
if iw.find(word) > -1:
return word
return''defmain():
iw = input('enter your words: ')
word = filterwords(iw)
if word != '':
print(iw.replace(word, '***'))
else:
print(iw)
if __name__ == '__main__':
main()